VueJS | How to get access to Router Parameter in my Component

bonblow picture bonblow · Jan 25, 2018 · Viewed 26.8k times · Source

I tried to get access to my parameter :id in my Entry component. I tried it with props but it doesn't work. Any idea? Can't find anything.

export default new Router({
    routes: [{
        path: '/entry/:id',
        name: 'Entry',
        component: Entry
    }]
})

Thank you

Answer

Russell Shingleton picture Russell Shingleton · Jan 25, 2018

While soju's answer is correct, I tend to use the method in which the docs refer to as 'decoupling the router using props'.

This can be accomplished by add the props: true option to your route and defining a property in your component.

So in your Route you would have:

export default new Router({
     routes: [{
          path: '/entry/:id',
          name: 'Entry',
          component: Entry,
          props: true
     }]
})

Then in your component you add a prop:

Vue.component('Entry', {
       template: '<div>ID: {{ id}}</div>',
       props:['id']
})

This can all be found in the docs: Passing Props to Route Components