vue.js-基于URL查询参数定义VUE路线的路径

发布于 2025-02-10 03:48:31 字数 518 浏览 0 评论 0原文

假设我有这个URL http:// localhost:8080/public/form?code = fhuz15ahy

我已经在我的route.js文件中定义了一条路由,例如

{ 
  path: '/public/form?code=:code',
  name: 'survey',
  component: () => import('./views/form')
}

,应该重复使用相同的组件基于代码查询来自URL的参数。

在我的form.vue组件中,我已经定义了这样的道具:

props: {
   code: {
       type: String,
       required: true
   }
}

但这是不起作用的,因为我没有重定向到任何页面。

我认为我在定义路线的路径方面做错了。我该如何实现?

Let's assume I have this URL
http://localhost:8080/public/form?code=fhuZ15aHy

I have defined a route in my route.js file like this

{ 
  path: '/public/form?code=:code',
  name: 'survey',
  component: () => import('./views/form')
}

which is supposed to reuse the same component based on the code query parameter coming from URL.

In my form.vue component I have defined a prop like this:

props: {
   code: {
       type: String,
       required: true
   }
}

But this is not working, in the sense that I am not redirected to any page.

I think I'm doing wrong with how I defined the path of the route. How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

固执像三岁 2025-02-17 03:48:31

请勿在路径中定义查询,

您应该像这样编写:

{ 
  path: '/public/form',
  name: 'survey',
  component: () => import('./views/form')
}
router.push({ path: '/public/form', query: { code } })

在组件中,您可以访问这样的查询

const {code} = this.$route.query

Do not define the query within the path

You should write like this:

{ 
  path: '/public/form',
  name: 'survey',
  component: () => import('./views/form')
}
router.push({ path: '/public/form', query: { code } })

And within the component you can access the query like this

const {code} = this.$route.query
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文