我想将数据作为参数发送,并在我用angularcript编写的angular2 app中的不同组件中检索该数据。我在发送类型为number的数据时成功,但在发送字符串数据时在不同情况下失败。在这种情况下,检索的数据是NaN。数据在URL中可见。我查看了angular2文档以及其他stackoverflow问题,但找不到解决方案。这些是相关的代码snippits。
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.
答案 0 :(得分:1)
您是否意识到+运算符正在尝试将字符串转换为数字? 所以如果一个字符串不是一个数字,它可能会返回NaN