响应路由参数的变化
当使用路由参数时,例如从 /user/foo 导航到 /user/bar,这两个路由对应的页面组件都由同一个组件渲染,如果使用销毁再创建未免太消耗资源,如果直接导航过去,vue生命周期钩子函数不会有任何执行,因此需要使用监听函数来监听。
watch: {
$route(to, from) {
// 对路由变化作出响应...
},
他与created函数所在位置同级.
或者使用导航守卫
// don't forget to call next()
beforeRouteUpdate (to, from, next) {
// react to route changes...
},