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
Another way to access the router props is to add an import statement at the top of the component and import 'withRouter'. import { withRouter } from 'react-router-dom'; Then in the export default statement at the end of the component you would wrap the component in 'withRouter'. export default withRouter(HomePage);
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
You should simply use $route.params.id
or this.$route.params.id
.
Read more : https://router.vuejs.org/guide/essentials/dynamic-matching.html
You should consider using props : https://router.vuejs.org/en/essentials/passing-props.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With