第一步
可以在登录页面的接口处写一个会话存储判断用户是否登录 代码如下
sessionStorage.setItem('token', true);
第二步
在main.js里面写全局守卫 代码如下
router.beforeEach((to, from, next) => {
const toPath = to.path;
// 将要进入的路径非'/login'
if (toPath !== '/') {
// 用户未登录
console.log(window.sessionStorage.getItem('token'));
if (!window.sessionStorage.getItem('token')) {
router.push('/').catch(err=>{err});
}
// 其他
else {
next();
}
}
// 将要进入的路径'/login'
else {
next();
}
});
仅供参考