Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine parameters from URL with props object in Vue router config

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 ?

like image 377
Adrien Castagliola Avatar asked Dec 18 '17 13:12

Adrien Castagliola


People also ask

How do I pass props through router link?

To recap, if you need to pass data from Link through to the new component that's being rendered, pass Link s a state prop with the data you want to pass through. Then, to access the Link s state property from the component that's being rendered, use the useLocation Hook to get access to location.

Can you pass Props to router view Vue?

Vue Router 4 provides multiple ways of doing this: from setting the props property on the route record to true and automatically passing all params as props, setting the props property to a static object to provide static props, or setting it to a function that returns the desired props.

How do I transfer data to Vue props?

The way it works is that you define your data on the parent component and give it a value, then you go to the child component that needs that data and pass the value to a prop attribute so the data becomes a property in the child component. You can use the root component (App.


1 Answers

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.

like image 72
Decade Moon Avatar answered Sep 30 '22 17:09

Decade Moon