在vue中監聽路由的方法有:1.使用watch方法監聽;2.使用watch方法監聽路由變化,并獲取路由信息;3.使用beforeEach方法全局監聽;4.使用beforeRoute方法在組件內部監聽
具體方法如下:
1.使用watch方法監聽路由
watch:{
$route(to,from){
console.log(from.path);
console.log(to.path);
}
}
2.使用watch方法監聽路由變化,并獲取路由信息
watch:{
$route:{
handler(val,oldval){
console.log(val);
console.log(oldval);
},
deep: true
}
}
3.使用beforeEach方法全局監聽路由
router.beforeEach((to, from, next) => {
console.log(to);
next();
});
export default router
4.使用beforeRoute方法在組件內部監聽路由
beforeRouteEnter (to, from, next) {
}
beforeRouteUpdate (to, from, next) {
}
beforeRouteLeave (to, from, next) {
}