vue 判斷是否登錄,未登錄跳轉到登錄頁


網頁一進入判斷是否登錄,未登錄跳轉到登錄頁面

router.js

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld,
      meta: {
        title: '首頁',
        requiresAuth: true // 是否需要判斷是否登錄
      }
    },
    {
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        title: 'login'
      }
    }
  ]
})

main.js

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  // 判斷該路由是否需要登錄權限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (to.name === 'login') {
      next()
    } else if (localStorage.getItem('token')) {
      // 訪問服務器緩存數據,判斷當前token是否失效
      Vue.http.get("http:xxxx/Login/UserIsLogin?token="+localStorage.getItem('token')+"&url="+to.name,{withCredentials: true}).then(response => response.json()).then(num => {
            // console.log('是否登錄',num);
            if(num.code==1001){
             next();
            } else{
             alert('您的token已超時,請重新登錄');
             next('/Login');
          }
       })
    } else {
      next('/Login')
    }
  } else {
    next()
  }
})


免責聲明!

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



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