-
plugins下新增route.js
1 /* 掛載導航路由守衛 */
2 export default ({ 3 app 4 })=>{ 5 // to 將訪問的路徑 6 // from 代表從那個路徑跳轉而來 7 // next 是一個函數,表示放行 next('/login') 強制跳轉 8 app.router.beforeEach((to, from, next) => { 9 if(to.path === '/login') return next(); 10 //獲取token 11 const tokenStr = window?.sessionStorage.getItem('token') 12 if(!tokenStr) return next('/login') 13 next() 14 }) 15 }
-
nuxt.config.js 中添加路徑
1 plugins: [{ 2 src: '~/plugins/element', 3 }, { 4 src: '~/plugins/route', 5 ssr: false, 6 }],