auth.js
let storage = window.localStorage;
let token = 'token';
export function setToken(tokens) {
return storage.setItem(token, tokens)
}
export function getToken() {
return storage.getItem(token)
}
router.js
import { getToken } from "@/utils/auth";
router.beforeEach((to, from, next) => {
if (to.name === 'login') return next();
let token = getToken();
if (!token) {
Message.warning("请先登录");
router.push({ name: 'login' });
return;
}
next()
})
网友评论