vue 設置路由的登錄權限


index.js

將需要登錄權限的路由設置meta屬性

meta:{requireAuth:true},  

main.js

在main.js內直接寫對路由的驗證

router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requireAuth)){ // 判斷該路由是否需要登錄權限
        if (sessionStorage.getItem("access_token")) { // 判斷當前的token是否存在
            next();
        }
        else {
            next({
            path: '/manage',
            query: {redirect: to.fullPath} // 將跳轉的路由path作為參數,登錄成功后跳轉到該路由
        })
       }
    }
    else {
        next();
    }
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM