一、watch监听路由的方法
通过watch监听,当路由发生变化的时候执行。
方法一:
watch:{
$router(to,from){
console.log(to.path)
}
}
方法二:
watch: {
$route: {
handler: function (val, oldVal){
console.log(val);
},
// 深度观察监听
deep: true
}
},
方法三:
watch: {
'$route' : 'getPath'
},
methods: {
getPath(){
console.log( this .$route.path);
}
}
二、key-用来阻止“复用”的
Vue 为你提供了一种方式来声明“这两个元素是完全独立的——不要复用它们”。只需添加一个具有唯一值的 key 属性即可(Vue文档原话)
computed: {
key() {
return this .$route.name !== undefined? this .$route.name + new Date(): this .$route + new Date()
}
}
使用computed属性和Dat