简体   繁体   English

带有子路径的Vue路由器

[英]Vue router with sub path

I have following component for the url:我有以下 url 组件:

  {
    path: '/resetpassword',
    name: 'view-resetpassword',
    component: ViewResetPassword,
  },

And it gets opened when I enter localhosst:8080/resetpassword当我输入localhosst:8080/resetpassword时它会打开

My question is how to access parameters that are given in that way:我的问题是如何访问以这种方式给出的参数:

localhost:8080/resetpassword/53c957fe-bbc2-4b32-8ebd-a84d79f7ffd2

I know, that I can access URL paramaters ?key=value with this.$route.query.key in the code.我知道,我可以在代码中使用this.$route.query.key访问 URL 参数?key=value But how do I access parameter /... ?但是我如何访问参数/...

You can use, like this你可以像这样使用

{ name: 'Projeto', path: '/projeto/:id', component: Projeto, meta: { public: true } }

with the:(the name), in the component:使用:(名称),在组件中:

this.$route.params.id

In my example is id, but you can put another name在我的示例中是 id,但您可以输入其他名称

A page to exampleVueRouter VueRouter示例页面

From the docs , proceed as so:docs中,按如下方式进行:

{
  path: '/resetpassword/:id',  // I changed here
  name: 'view-resetpassword',
  component: ViewResetPassword,
}

Then in the matched component, you can access the parameter through $route.params.id .然后在匹配的组件中,您可以通过$route.params.id访问参数。

Set your router to:将您的路由器设置为:

{
  path: '/resetpassword/:token?',
  name: 'view-resetpassword',
  component: ViewResetPassword,
},

That route now can have optional token param ( ? is for the optional).该路由现在可以具有可选的token参数( ?是可选的)。 Then you can access it on your component然后你可以在你的组件上访问它

this.$route.params.token

You can also pass it as a props instead, by adding props: true on the router您也可以通过在路由器上添加props: true来将其作为道具传递

{
  path: '/resetpassword/:token?',
  name: 'view-resetpassword',
  component: ViewResetPassword,
  props: true
},

and on your component在你的组件上

export default {
  ...
  props: ["token"];
  ...
}

then you can access the props然后你可以访问道具

this.token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM