Kees van Lierop
Kees van Lierop

Reputation: 1013

vue-router: Optional fixed + path parameter

I want to have one route object for the following scenarios:

What I have tried, is:

{ 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. 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?

Upvotes: 1

Views: 1990

Answers (1)

mikeemay
mikeemay

Reputation: 59

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

For example:

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

Then your navigation to SomeComponent should have something like:

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

Upvotes: 4

Related Questions