问题:访问项目地址http://localhost:8080/receivingMode。在vue实例挂载页面mounted生命周期获取this.$route.path获取到的值是/
//router
const routes = [
{
path: '/',
name: 'Home',
component: () => import('@/views/home.vue')
}
]
//App.vue
mounted() {
console.log(this.$route.path)
}
原因:异步引入路由 同步引入路由不会存在这个问题
解决方法:
//1、computed赋值
computed:{
keepAlive(){
return this.$route.path;
}
},
//2、watch监听路由
watch:{
'$route': {
immediate: true,
deep: true,
handler(val){
console.log(this.$route.path);
}
}
}