Angular2 this.route.params.map

时间:2016-10-26 21:16:23

标签: angular angular2-routing

Angular2我是懒惰加载模块并设置子路由如下

module.ts

中的

const routes: Routes = [
    { path: '', component: ProgramsComponent},
    { path: 'create/:id', component: CreateProgramComponent}

];

这里是利用“创建”路线的链接

<a [routerLink]="['create','123']" class="btn btn-primary">New</a>
尝试提取id值的接收器组件中的

constructor(private route: ActivatedRoute, private programService:ProgramService) {

    this.route.params
        .map(params => params['id'])
        .switchMap(id => this.programService.getProgramById(id))
        .subscribe(program => this.model = program);
}

我收到以下错误,感谢任何帮助

core.umd.js:3462 EXCEPTION: Uncaught (in promise): TypeError: this.route.params.map is not a function
TypeError: this.route.params.map is not a function

1 个答案:

答案 0 :(得分:10)

添加以下两行:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';

RxJs由运营商引入运营商,以确保您只加载您需要的东西。 在您的情况下,您使用两个运算符; mapswitchMap