Rumah  >  Soal Jawab  >  teks badan

javascript - vue-router auth验证怎么做?

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!auth.loggedIn()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

这段代码写在router.js中,那么,auth.loggedIn()是怎么定义的?又是定义在那里?

router.beforeEach((to, from, next) => {
    // 取vuex中的state的值
  if (!store.state.auth) {
    if (to.path !== '/login') {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
});

this.axios.get('/platform/login/islogin/')
        .then((result) => {
          var result = result.data
          if (!result.code) {
            this.$store.commit('handleUserState')
            return true
          } else {
            this.$router.push('/login')
            return false
          }
        })

直接取vuex中的state值,auth值是会发起请求进行判断赋值,默认是false,那么beforeEach进行的时候,取的还是原来的值,不是请求回来赋值的值,想请求auth验证有什么其他方案?

Thanks you in advance.

怪我咯怪我咯2771 hari yang lalu426

membalas semua(1)saya akan balas

  • 黄舟

    黄舟2017-04-11 13:02:00

    请看https://github.com/fengyincha...

    balas
    0
  • Batalbalas