響應路由參數的變化
當使用路由參數時,例如從 /user/foo 導航到 /user/bar,這兩個路由對應的頁面組件都由同一個組件渲染,如果使用銷燬再創建未免太消耗資源,如果直接導航過去,vue生命週期鉤子函數不會有任何執行,因此需要使用監聽函數來監聽。
watch: {
$route(to, from) {
// 對路由變化作出響應...
},
他與created函數所在位置同級.
或者使用導航守衛
// don't forget to call next()
beforeRouteUpdate (to, from, next) {
// react to route changes...
},