0

Can anybody tell me if there is a way to access the route params directly inside the route objects?, I need to require different components based on the route object.

router.map({
  '/dashboard/*': {
    component: Home
  },
  '/dashboard/:module': {
    component: Module,
    subRoutes: {
      '/': {
        component: require('./components/'+this.$route.params.module+'.vue')
      },
      '/:tab': {
        component: require('./components/'+this.$route.params.tab+'.vue')
      }
    }
  }
});

2 Answers 2

3

You can't require one or another component depending on the route parameters directly in the router configuration. What you can do instead is create a wrapper component that will use a component or another depending on the route parameters, as the route parameters are accessible from any components.

As written in the vue-router documentation:

The route object will be injected into every component in a vue-router-enabled app as this.$route, and will be updated whenever a route transition is performed.

Every of your components have access to the current route through this.$route therefore you could do something like this:

<template>
    <component-a v-if="$route.params.param1 === 'a'></component-a>
    <component-b v-if="$route.params.param1 === 'b'></component-b>
</template>
Sign up to request clarification or add additional context in comments.

2 Comments

As jeerbl said, you should check parameters on the view. However I'd recommend you using a render function instead of a template
Thank you, I was asking because I saw something similar in Voie router github.com/inca/voie-example/blob/master/spa/states/user/…
2

I haven't seen the use of $route.params inside the route objects. What you can do though, in your vue you can dynamically load different components based on your $route.params

<component-a v-if="$route.params == 'a'"></component-a>

Comments

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.