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

怪我咯
怪我咯

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

Antworte allen(1)
黄舟

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

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!