nuxt全局掛載導航路由守衛


  • 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   }],

 


免責聲明!

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



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