vue 路由守衛,未登錄強制跳轉到登錄頁


main.js 中,

//  掛載路由導航守衛
router.beforeEach((to, from, next) => {
   //獲取token const hasToken = getToken(); // to 將要訪問的路徑 // from 代表從哪個路徑跳轉而來 // next 是一個函數,表示放行 // next() 放行 next('/login') 強制跳轉 // 如果用戶訪問的登錄頁,直接放行 if (hasToken) { if (to.path === '/login') { next({ path: '/' }) NProgress.done() } next() } else { // 沒有token,強制跳轉到登錄頁 if (whiteList.indexOf(to.path) !== -1) { next() } else { next(`/login`) // next(`/login?redirect=${to.path}`) NProgress.done() } } })

  


免責聲明!

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



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