[英]Why does it not work to retrieve parameter data in my angular2 app?
我想将数据作为参数发送,并在用打字稿编写的angular2应用程序的其他组件中检索该数据。 在发送数字类型的数据时,我可以成功完成此操作,但是在发送字符串数据时,在不同情况下,它会失败。 在这种情况下,检索到的数据为NaN。 数据在URL中可见。 我查看了angular2文档以及其他stackoverflow问题,但找不到解决方案。 这些是相关的代码片段。
app.routes.ts
{ path: 'recommendations', component: Recommendations,
children:[
{path:'info/:mod-title/:mod-desc', component:ModalInfo},
{path:'map/:lat/:lng', component:Map}
]},
Recommendation.component.ts
loadInfo(){
this.router.navigate(['/recommendations/info/:'+this.modTitle+'/:'+this.modDesc]);
loadMap(){
this.router.navigate(['/recommendations/map/:'+this.lat+'/:'+this.lng]);
}
modal-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.
您是否意识到+运算符正在尝试将字符串转换为数字? 因此,如果字符串不是数字,则可能会返回NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.