Angular2 this.route.params.map [英] Angular2 this.route.params.map

查看:115
本文介绍了Angular2 this.route.params.map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular2我是延迟加载模块,并按照以下步骤设置子路由

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

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>

在接收器组件中尝试按以下方式提取ID值

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

推荐答案

添加以下两行:

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

RxJs是由操作员引入的,以确保您仅加载所需的内容. 在您的情况下,您使用的是两个运算符; mapswitchMap

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

这篇关于Angular2 this.route.params.map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆