Vue watch handle 方法 监听$route
watch 监听路由对象 $route
// 监听路由
watch:{
'$route': {
handler ({ fullPath }) { // $route对象内部的fullPath
this.active = fullPath
this.$nextTick(() => { //异步执行
if (this.aside.length > 0 && this.$refs.menu) {
this.$refs.menu.activeIndex = fullPath
}
})
},
immediate: true
}
}
- handle 就是你watch中需要具体执行的方法;
- deep:就是你需要监听的数据的深度,一般用来监听对象中某个属性的变化;
- immediate:在选项参数中指定 immediate: true 将立即以表达式的当前值触发回调;