beforeEnter Vue路由器,使用Next()Vuex进行无限循环

时间:2020-07-09 15:24:53

标签: javascript vue.js asynchronous vuex vue-router

好吧,我正在使用路由,在进入之前输入监护人,这个想法是,如果您没有通过身份验证,它将重定向您到登录名,但是如果您通过了身份验证并且转到登录名,它将将您重定向到仪表板或主页。

当您未通过身份验证并带您进入登录时,会发生错误,该登录具有与监护人相同的功能,但具有区分它的参数,因此在输入第二个next()时启动无限循环且不会完成解决。

这是我的路线的定义。

Login Route definition image

Dashboard Route definition image

这里是beforeEnter函数,基本上我正在做的是运行一个返回用户的调度。 个人资料。

/*
    This will cehck to see if the user is authenticated or not.
*/
function requireAuth(to, from, next) {

  store.dispatch(USER_REQUEST); // Get user profile

  store.watch(() => store.getters.getProfile, profile => {

    // Strict
    if (to.matched.some(record => record.meta.requireAuth)) { // Pages that require strict authentication

      // If you aren't logging, redirect to login page
      if ((store.getters.isAuthenticated === false &&
        store.getters.isProfileLoaded === false)) {

        next({
          path: '/login',
          meta: {
            requireAuth: false
          }
        });

      } else {
        next();
      }

    } else { //Pages that are hidden when you log in.

      
      // If you are logging, redirect to main page.
      if (store.getters.isAuthenticated === true && store.getters.isProfileLoaded === true && profile.verified) {

        next({
          path: '/dashboard',
        });

      } else {
        next();
      }
      
    }

  })

} 

0 个答案:

没有答案