案例
watch: { $route: { handler: function(route) { console.log(route); const query = route.query if (query) { this.redirect = query.redirect this.otherQuery = this.getOtherQuery(query) } },
//deep:
true
immediate: true } },
首先 watch是 vue內部提供的一個用於偵聽功能的更通用的方法,其用來響應數據的變化,通過特定的數據變化驅動一些操作
$route 是當前路由信息對象
handler:是一個回調函數。即監聽到變化時應該執行的函數。里面有兩個參數 一個 是newValue變化后新的值 oldValue變化前新的值
watch 和$route就是來監聽路由的動態變化的
deep:其值是true或false;確認是否深入監聽。(一般監聽時是不能監聽到對象屬性值的變化的,數組的值變化可以聽到。) 可以監測多層級結構的數據
immediate:其值是true或false; 初始化時立即執handler的函數。