返回 NaN 的 Angular2 url 参数



改写我之前的问题,我有这条路线,它给了我这样的东西/order-screen/store/1/London

{
path: 'order-screen/store/:storeId/:storeLocation',
component: OrderComponent
}

当我尝试使用this.route.params获取storeLocation时,它返回错误NaN.我想London获取位置,不确定.params是否是正确的使用位置。

this.routeSub = this.route.params.subscribe(params => {
this.storeLocation = +params['storeLocation'];
console.log('Location >> ', this.storeLocation);
})

只需从参数之前删除+即可。

所以..
this.routeSub = this.route.params.subscribe(params => {
this.storeLocation = params['storeLocation'];
console.log('Location >> ', this.storeLocation);
})

+会将字符串转换为数字,由于"伦敦"不是数字,因此您会收到 NaN 错误

最新更新