1

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

  • /a/123/b
  • /b

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?

2
  • 2
    Express Router and Vue Router both are a different entity. Commented Mar 31, 2019 at 7:27
  • They both use github.com/pillarjs/path-to-regexp according to the docs. I added the Express route tester example to illustrate the change in behavior in the different path-to-regexp versions. Commented Mar 31, 2019 at 16:14

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Of course they are different, but under the hood they both use path-to-regexp github.com/pillarjs/path-to-regexp. I added the Express route tester example to illustrate that what you are suggesting does work for path-to-regexp@0.1.7, but not for >=1.0, which is what vue-router is using. As mentioned in my question, I have tried the answer in your suggestion, but because the newest version of path-to-regexp encodes special characters, it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.