【问题标题】:How to pass props from URL in VueJS when redirect in vue-router?在 vue-router 中重定向时如何从 VueJS 中的 URL 传递道具?
【发布时间】:2021-11-17 12:44:23
【问题描述】:

我有在 4 秒内重定向用户的功能,但我需要以某种方式传递道具:

    setTimeout(() => {
      clearInterval(intervalId)
      this.$router.push({ path: `/${this.$store.state.locale}/my-cart` })
    }, 4000)

我试图做这样的事情:

this.$router.push({ name: '/${this.$store.state.locale}/my-cart', params: {step: '2'} })

这不起作用,该路由在主页上重定向,那么,我该怎么做?

【问题讨论】:

  • 您是否尝试将选项props: true 添加到路由配置中?检查这个:router.vuejs.org/guide/essentials/…
  • 其实我错了,问题也错了。道具是关于组件的,但我有静态 url /my-cart。所以,这不会以这种方式工作,但是谢谢

标签: vue.js routes vue-router


【解决方案1】:

如果你想使用 url 来声明一个路径。您需要将“名称”更改为“路径”。

this.$router.push({ path: '/${this.$store.state.locale}/my-cart', params: {step: '2'} })

如果你想使用 name,你可以传递在 route.js 文件中定义的 name 属性的值,而不是传递 url。

this.$router.push({ name: 'namePath', params: {step: '2'} })

【讨论】:

    【解决方案2】:
    this.$router.push({ path: '/${this.$store.state.locale}/my-cart', params: {step: '2' }})
    

    请更改name 并将path 替换为代码

    【讨论】:

      猜你喜欢
      • 2019-09-01
      • 2019-09-12
      • 2017-09-05
      • 2018-06-23
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2020-10-19
      相关资源
      最近更新 更多