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() } } })