简体   繁体   English

vue-router:可选的fixed + path参数

[英]vue-router: Optional fixed + path parameter

I want to have one route object for the following scenarios: 我想在以下情况下使用一个路由对象:

  • /a/123/b
  • /b

What I have tried, is: 我尝试过的是:

{ path: '(a/:a)?/b', ... } {path:'(a /:a)?/ b',...}

This seems to work when testing the path on the Express route tester , but only for path-to-regexp version 0.1.7. Express路由测试器上测试路径时,这似乎可行,但仅适用path-to-regexp版本0.1.7。 Any version above that will escape special characters. 上面的任何版本都将转义特殊字符。

In what way is this possible with the new version of path-to-regexp that vue-router uses? vue-router使用的新版本的path-to-regexp可以通过什么方式实现?

Express Router and Vue Router are different, but if what you mean is you want to create a route with dynamic url, then perhaps you can use named routes from https://router.vuejs.org/guide/essentials/named-routes.html Express Router和Vue Router不同,但是如果您要使用动态URL创建路由,则可以使用https://router.vuejs.org/guide/essentials/named-routes中的命名路由。 HTML

For example: 例如:

 const router = new VueRouter({
  routes: [
    {
      path: '(a/:a)?/b',
      name: 'a',
      component: SomeComponent
    }
  ]
})

Then your navigation to SomeComponent should have something like: 然后,您对SomeComponent的导航应类似于:

<router-link :to="{ name: 'a', params: { a: 123 }}">SomeComponent</router-link>

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

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