使用场景:
组件复用;路由跳转;
beforeRouteUpdate (to, from, next) { // 在当前路由改变,但是该组件被复用时调用 // 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候, // 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。 // 可以访问组件实例 `this` },
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) } }) } } }