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


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

router.js


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

main.js


router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  const type = to.meta.type
  // 判斷該路由是否需要登錄權限
  if (type === 'login') {
    if (window.localStorage.getItem('login')) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()  // 確保一定要有next()被調用
  }
})

來源:https://segmentfault.com/a/1190000016889438


免責聲明!

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



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