为什么在我的 angular2 应用程序中检索参数数据不起作用?
Why does it not work to retrieve parameter data in my angular2 app?
我想将数据作为参数发送并在我用打字稿编写的 angular2 应用程序的不同组件中检索该数据。我在发送数字类型的数据时成功了,但在发送字符串数据时在不同的情况下失败了。在这种情况下,检索到的数据是 NaN。数据在 URL 中可见。我查看了 angular2 文档以及其他 Whosebug 问题,但找不到解决方案。这些是相关的代码片段。
app.routes.ts
{ path: 'recommendations', component: Recommendations,
children:[
{path:'info/:mod-title/:mod-desc', component:ModalInfo},
{path:'map/:lat/:lng', component:Map}
]},
recommend.component.ts
loadInfo(){
this.router.navigate(['/recommendations/info/:'+this.modTitle+'/:'+this.modDesc]);
loadMap(){
this.router.navigate(['/recommendations/map/:'+this.lat+'/:'+this.lng]);
}
模态-info.component.ts
ngOnInit():any {
this.sub = this.modalRoute
.params
.subscribe(params => {
this.itemTitle = +params['mod-title'];
this.itemDesc = +params['mod-desc'];
});
alert(this.itemTitle+" "+this.itemDesc);
this.loadData();
} //This alerts NaN
google.component.ts
ngOnInit():any {
this.sub = this.route
.params
.subscribe(params => {
this.lat = +params['lat'];
this.lng = +params['lng'];
});
} //This assigns the data correctly.
你知道 + 运算符试图将字符串转换为数字吗?
所以如果一个字符串不是数字,它可能 return NaN
我想将数据作为参数发送并在我用打字稿编写的 angular2 应用程序的不同组件中检索该数据。我在发送数字类型的数据时成功了,但在发送字符串数据时在不同的情况下失败了。在这种情况下,检索到的数据是 NaN。数据在 URL 中可见。我查看了 angular2 文档以及其他 Whosebug 问题,但找不到解决方案。这些是相关的代码片段。
app.routes.ts
{ path: 'recommendations', component: Recommendations,
children:[
{path:'info/:mod-title/:mod-desc', component:ModalInfo},
{path:'map/:lat/:lng', component:Map}
]},
recommend.component.ts
loadInfo(){
this.router.navigate(['/recommendations/info/:'+this.modTitle+'/:'+this.modDesc]);
loadMap(){
this.router.navigate(['/recommendations/map/:'+this.lat+'/:'+this.lng]);
}
模态-info.component.ts
ngOnInit():any {
this.sub = this.modalRoute
.params
.subscribe(params => {
this.itemTitle = +params['mod-title'];
this.itemDesc = +params['mod-desc'];
});
alert(this.itemTitle+" "+this.itemDesc);
this.loadData();
} //This alerts NaN
google.component.ts
ngOnInit():any {
this.sub = this.route
.params
.subscribe(params => {
this.lat = +params['lat'];
this.lng = +params['lng'];
});
} //This assigns the data correctly.
你知道 + 运算符试图将字符串转换为数字吗? 所以如果一个字符串不是数字,它可能 return NaN