I’m learning Vue router. And I want to made programmatic navigation without using <router-link>
in templates file.
My router and view:
JavaScript
x
19
19
1
router = new VueRouter({
2
routes: [
3
{path : '/videos', name: 'allVideos', component: Videos },
4
{path : '/videos/:id/edit', name: 'editVideo', component: VideoEdit },
5
]
6
});
7
8
new Vue({
9
el: "#app",
10
router,
11
created: function(){
12
if(!localStorage.hasOwnProperty('auth_token')) {
13
window.location.replace('/account/login');
14
}
15
16
router.push({ name: 'allVideos' })
17
}
18
})
19
So by default I push to ‘allVideos’ route and inside that component I have a button and method for redirecting to ”editVideo’ button:
JavaScript
1
2
1
<button class="btn btn-sm btn-warning" @click="editVideo(video)">Edit</button>
2
method:
JavaScript
1
2
1
editVideo(video) {router.push({ name: 'editVideo', params: { id: video.id } })},
2
It works fine. But when I try to get id inside a VideoEdit component using $route.params.id
I got error Uncaught ReferenceError: $route is not defined
Maybe it’s because I’m not using npm for now just a cdn version of Vue and Vuerouter. Any solutions? Thanks!
Updated: btw in Vue dev tool I see $route instance inside the component
Updated:
JavaScript
1
15
15
1
var VideoEdit = Vue.component('VideoEdit', {
2
template: ` <div class="panel-heading">
3
<h3 class="panel-title">Edit {{vieo.name}}</h3>
4
</div>`,
5
data() {
6
return {
7
error: '',
8
video: {},
9
}
10
},
11
created: function () {
12
console.log($route.params.id);
13
},
14
})
15
Advertisement
Answer
Thanks to Sandeep Rajoria
we found solution, need to use this.$route
except $route
inside a component