【问题标题】:How can I redirect localpath to another page with params Nuxt Js如何使用参数 Nuxt Js 将 localpath 重定向到另一个页面
【发布时间】:2021-09-02 22:13:58
【问题描述】:

您好,我有一些问题我想将 url 中的参数推送到 NuxtJs 中的另一个页面

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

methods: {
    fetchpage() {
      const orderId = this.idPending;
      this.$router.push(
        this.localePath({
          name: 'Checkout-Confirmation-orderId', // page with params orderId
          params: { orderId: orderId }
        })
      );
    }
  }

当我这样做时,我遇到了这个错误,我不明白为什么

Argument type {name: string, params: {orderId: Number}} is not assignable to parameter type RawLocation   Type Number is not assignable to type string 

请帮忙谢谢

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    这个错误确实是不言自明的,你不能在这里使用String以外的类型。
    可以在此处找到确认:https://github.com/vuejs/vue-router/issues/2895#issuecomment-523549070

    您可以使用String(orderId),据我所知,它可能更快,但主要是更安全,因为如果您的orderId 为空或未定义,它不会产生任何错误。

    【讨论】:

      【解决方案2】:

      我回答我的问题

      在页面之间可以作为参数传递的所有内容始终是 String 类型,在我的情况下,我的 props id 是 Number 类型,因此错误我将类型转换为这样的参数中的字符串

      fetchpage() {
            const orderId = this.idPending;
            this.$router.push(
              this.localePath({
                name: 'Checkout-Confirmation-orderId', // page with params orderId
                params: { orderId: orderId.toString() }
              })
            );
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-02
        • 2013-08-17
        • 2022-06-16
        • 1970-01-01
        相关资源
        最近更新 更多