You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt.js使用localePath跳转传参时报类型错误如何解决?

问题解决方法

错误原因

报错核心原因是路由参数类型不匹配:Nuxt.js 底层依赖的 Vue Router 要求路径占位符对应的 params 参数必须为字符串类型,因为 URL 路径片段本身仅支持字符串格式。你当前传入的 orderId 是数字类型,触发了类型校验规则所以抛出错误。

解决方案

直接在传参时将数字类型的 orderId 转为字符串即可,修改后的代码如下:

props: {
    idPending: {
      type: Number,
      required: true
    }
},

methods: {
    fetchpage() {
      // 数字转字符串,也可以用模板字符串写法:const orderId = `${this.idPending}`
      const orderId = this.idPending.toString();
      this.$router.push(
        this.localePath({
          name: 'Checkout-Confirmation-orderId', // page with params orderId
          params: { orderId: orderId }
        })
      );
    }
  }

如果需要在目标页面使用数字类型的 orderId,在目标页面接收参数后通过 Number(this.orderId) 转换即可。

内容的提问来源于stack exchange,提问作者francky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 06:51:03