【发布时间】:2017-05-02 09:18:20
【问题描述】:
我正在开发一个 angular2 应用程序。
我想与我在承诺中得到的参数一起使用。
我不断收到此错误:
异常:未捕获(承诺中):错误:http://localhost:5000/js/angular-client/app/components/some/some.component.html:0:7 中的错误由以下原因引起:c 为空 normalizeCommands/_loop_1@http://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:2384:15 normalizeCommands@http://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:2427:11 createUrlTree@http://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:2284:49 路由器http://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:3486:18 RouterLinkWithHrefhttp://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:4577:22 RouterLinkWithHrefhttp://localhost:5000/js/angular-client/node_modules/@angular/router/bundles/router.umd.js:4570:11 RouterLinkWithHref
这是我的代码:
some.component.ts:
someAnswer: SomeAnswer;
ngOnInit(): void {
this.sub = this.route.params.subscribe(params => {
this.getPromise(+params['id']);
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
getPromise(id: number): Promise<SomeAnswer> {
return this.http.get('/api/'+id+'')
.toPromise()
.then(response => this.someAnswer = response.json() as SomeAnswer)
.catch(this.handleError);
}
一些.component.html:
<h2><a [routerLink]="['/url', someAnswer?.id]">banana</a></h2>
所以我的问题是 - 如何将 [routerLink] 与我从承诺中获得的参数一起使用?
【问题讨论】:
标签: angular angular2-routing angular2-template