Angular2 URL参数返回NaN [英] Angular2 url params returning NaN

查看:41
本文介绍了Angular2 URL参数返回NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重述我以前的问题,我有一条路线可以给我类似这样的信息/order-screen/store/1/伦敦

Rewording my previous question, I have this route which gives me something like this /order-screen/store/1/London

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

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

When I try to get the storeLocation using this.route.params it returns an error of NaN. I want to get the location London, not sure if .params is the right one to use.

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

推荐答案

只需从参数之前删除 + .所以..

Just remove the + from before your params. So..

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

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

The + will convert a string to a number and since "London" is not a number you got the NaN error

这篇关于Angular2 URL参数返回NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆