1. 程式人生 > >vue-router 判斷是否登陸,未登入跳轉登陸頁面

vue-router 判斷是否登陸,未登入跳轉登陸頁面

移動app 只需驗證首頁是否登陸

meta: {
  title: 'index',
  requireAuth: true
}

main.js中

router.beforeEach((to, from, next) => {
  if (to.matched.some(m => m.meta.requireAuth)) {
    console.log(store.getters.token)
    // 對路由進行驗證
    if (store.getters.token) { // 已經登陸
      next() // 正常跳轉到你設定好的頁面
    } else {
      // 未登入則跳轉到登陸介面
      next({ path: '/login' })
    }
  } else {
    next()
  }
})