vue 路由守衛及elementUI 的Message在main.js裏的使用

  • vue 提供了路由改變前後的路由導航守衛,在改變前後的兩種方法是:
  • 全局前置守衛:
    const router = new VueRouter({ ... })
    
    router.beforeEach((to, from, next) => {
      // ...
    })

     

  • 全局後置守衛
    router.afterEach((to, from) => {
      // ...
    })

     

  • 一個調用後臺接口驗證是否登錄的寫法:
    import { Message } from 'element-ui';
    Vue.prototype.$message = Message;
    
    
    router.afterEach((to, from) => {
        debugger
        this.url=///校驗是否登錄url
        axios({
            method: 'get',
            url: this.url
        }).then(function (res) {
         
                if (res.data.code ==0) {
                    Message({
                        message: "用戶未登錄",
                        type: 'waring'
                    })
                    window.location.href = //登錄url
                }
    
           
        }).catch(function (e) {
            root.$message({
                message: e.message,
                type: 'error'
            })
        });
        console.log("調用後")
        // ...
    })

     

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