Vue Router Guard方法仍然允许加载页面。

问题描述 投票:1回答:1

我使用了下面的守护常量来防止用户在没有保存cookie的情况下进入页面。但是,如果我尝试了足够多的时间,用户可以进入页面,但组件没有加载,只是一个空白的页面。

我还应该在我的路由器文件中添加什么来防止这种事情的发生?

这是我的防护常量。

const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
    this.$store.dispatch('logout')
    window.location.href = "/";
  } else {
    next();
  }
};
vue.js vuejs2 vue-component vuex vue-router
1个回答
1
投票

尝试使用 next('/') 而不是 window.location.href = "/" :

import store from 'path/to/store/
const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
   store.dispatch('logout');
    next('/');
  } else {
    next();
  }
};
© www.soinside.com 2019 - 2024. All rights reserved.