Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue router /:param with slashes

I have a router like this:

routes: [
    {
      path: '/survey/:surveyHash',
      name: 'survey',
      component: Survey,
      props: true,
    },

In the component I'm trying to get surveyHash in props. The trouble is I can't do it when there are / slashes inside surveyHash.

Is there a solution in vue-router?

like image 201
Adam Orłowski Avatar asked Dec 21 '18 20:12

Adam Orłowski


2 Answers

You can use a custom matching pattern in your route definition

routes: [
    {
      path: '/survey/:surveyHash(.*)',
      name: 'survey',
      component: Survey,
      props: true,
    },

With this you will get the rest of the URL including slashes in surveyHash

like image 187
nemesv Avatar answered Oct 05 '22 21:10

nemesv


There is more elegant solution. Vue router uses path-to-regexp module under the hood and it have solution for this out of the box

const regexp = pathToRegexp('/:foo*')
// keys = [{ name: 'foo', delimiter: '/', optional: true, repeat: true }]

https://github.com/pillarjs/path-to-regexp#zero-or-more

const regexp = pathToRegexp('/:foo+')
// keys = [{ name: 'foo', delimiter: '/', optional: false, repeat: true }]

https://github.com/pillarjs/path-to-regexp#one-or-more

like image 40
PayteR Avatar answered Oct 05 '22 22:10

PayteR