Search code examples
vue.jsnuxt.jsvue-router

Can vue routes have a default value for a parameter?


Imagine a route with the following path: ':uuid*/form/:step'

Can we specify the default value of the step parameter, on the router definition?

I've tried setting this value on the view's created function but I'm trying to avoid a redirect.


Solution

  • You can achieve something like this using router navigation guard:

    routes = [
      {
        name: 'FormStep',
        path: ':uuid*/form/:step'
        beforeEnter(route) {
          return { 
            path: route.path, 
            params: {
              ...route.params,
              step: route.params.step ?? defaultStep
            }
          }
        }
      }
    ]