我使用了下面的守护常量来防止用户在没有保存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();
}
};
尝试使用 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();
}
};