1.watch监听$route新值和旧值是否相等
watch:{ '$route.path':function(newValue,oldValue){ if(newValue=='xxx'){ } } }
2.利用beforeRouteUpdate钩子函数监听解决
export default{
data(){
return {}
},
beforeRouteUpdate(to,from,next){
console.log(to,from,next)
if(to.fullPath!=from.fullPath){
next()
this.changeUser()
}
},
methods:{
changeUser(){
getUserBaseInfo({userId:this.$route.params.id}).then(res=>{
if(res.success){
this.stuInfo = res.data;
}else{
this.$message.error(res.message)
}
})
}
}
}
3.重写push方法解决(复制到main.js里面)
import Vue from 'vue'
import VueRouter from 'vue-router'
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
return routerPush.call(this, location).catch(error => error)
}