问题
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