路由切换不触发created等钩子函数
问题来源
在同一个component下的组件来回切换时,不会触发created和mounted钩子函数
解决方式
1.watch $route
watch:{ //监听路由变化
$route( to , from ){
console.log( to , from )
// to , from 分别表示从哪跳转到哪,都是一个对象
// to.path ( 表示的是要跳转到的路由的地址 eg: /home );
}
}
2.router-view上加上一个唯一的key
//简单的在 router-view上加上一个唯一的key,来保证路由切换时都会重新渲染触发钩子了
<router-view :key="key"></router-view>
computed: {
key() {
return this.$route.name !== undefined? this.$route.name + +new Date(): this.$route + +new Date()
}
}