7

Here is my router config.

const router = new VueRouter({
  routes: [
   {
      path: '/user/:id',
      components: User,
      props: {
        sidebar: true 
      }
    }
  ]
})

I can't access to all props (e.g sidebar and id), only sidebar in this config.

Any ideas ?

2 Answers 2

9

If I understand you correctly, you want to set the prop sidebar to true (static) and the prop id to the :id URL parameter (dynamic).

You'll have to use a function which takes the route as a parameter and returns the prop values you want to set on the component:

props: route => ({
  id: route.params.id,
  sidebar: true,
})

Have a look at Function Mode in the vue-router docs for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Is this still possible if you have multiple components and need to pass different sets of props to different named components?
0

You can access route path and params like this:

this.$route.path or

this.$route.params.id

from within your function, computed props or your template

4 Comments

Is there any other way ? From Vue's doc, it seems to be not the best way to do it.
Do you want to pass ` props: { sidebar: true }` to User component?
i do, i need it in my User component.
can you check jsfiddle.net/ow85z2n8/1 ? I can get the id in the template

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.