Angular2 this.route.params.map

故事扮演 提交于 2019-12-10 18:28:55

问题


Angular2 i am lazy loading module and setting up child routes as following

in module.ts

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

];

here is link utilizing "create" route

<a [routerLink]="['create','123']" class="btn btn-primary">New</a>

in the receiver component trying to extract id value as following

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);
}

I am getting following error, any help is appreciated

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:


Add the following two lines:

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

RxJs is brought in operator by operator to ensure that you only load what you need. In your case you are using two operators; map and switchMap



来源:https://stackoverflow.com/questions/40272258/angular2-this-route-params-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!