javascript - vue-router auth验证怎么做?
怪我咯
怪我咯 2017-04-11 13:00:00
0
1
349
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.

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
黄舟

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!