VUE 結合 router 實現全局攔截,驗證用戶是否登錄,沒有登錄的話,跳到登錄界面


1、在 router.js 里面 添加 路由攔截內容:

router.beforeEach((to,from,next)=>{
  if(to.path ==='/login'){
    next();
  }else {
    let token = localStorage.getItem('userInfo');
    if(token === null || token === ''){
      next('/login');
    }else {
      next();
    }
  }
});

 


免責聲明!

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



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