vue router 總結

一、組件內導航之 beforeRouteUpdate 的使用

使用場景:

  組件複用;路由跳轉;

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);
                }
            
            });
        },  
    },  
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章