入口文件:
在监听路由改变前,对token进行判断,当需要跳转到登录页时,需要带上上一个页面的路由信息
router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = getPageTitle(to.meta.title);
const hasToken = getToken();
let userInfo = getUserInfo();
if (hasToken) {
if (to.path === '/login') {
// if is logged in, redirect to the home page
next({ path: '/' });
NProgress.done();
} else {
const hasUserInfo = store.getters.userInfo;
if (hasUserInfo && hasUserInfo.length > 0) {
next();
} else {
try {
userInfo = JSON.parse(userInfo);
const accessRoutes = await store.dispatch(
'permission/generateRoutes',
userInfo.accountType
);
store.state.user.userInfo = JSON.stringify(userInfo);
router.options.routes = accessR