【问题标题】:Vue: Redirect an user on logout depeding on which type of page it isVue:根据用户的页面类型在注销时重定向用户
【发布时间】:2020-11-01 13:48:09
【问题描述】:

如果未通过身份验证,我会使用导航保护来禁止用户访问管理页面。

router.beforeEach(function(to, from, next){
  if (to.meta.requiresAuth && !store.getters.isAuthenticated) {
    next({name: 'site-login'});
  }
  else {
    next()
  }
});

requiresAuth 用作管理页面上的元数据,isAuthenticated 是一个 getter,用于在 store(Vuex) 中检查用户的状态。

当用户单击注销按钮时,我想检查用户的位置。如果它在并且管理页面重定向到/ 如果它在正常页面上,只需重新加载当前页面。

我想使用与上面类似的东西。

 <button @click="logout">Logout</button>

 methods: {
      logout() {
        this.$store.dispatch('logout');
      }
    }

【问题讨论】:

    标签: vue.js vuex vue-router


    【解决方案1】:

    logout 创建一个特殊路由,并从那里分派logout 操作:

    {
      path: '/logout',
      name: 'logout',
      beforeEnter (to, from, next) {
        store.dispatch('logout').then(() => {
        if (from.meta.requiresAuth) {
            return next('/');
        }
          location.reload();
        }
      }
    },
    

    通过将用户重定向到此页面来注销:

    <button>
      <router-link to="/logout">Logout</router-link>
    </button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多