70 lines
1.5 KiB
TypeScript
70 lines
1.5 KiB
TypeScript
|
import {
|
||
|
createRouter,
|
||
|
createWebHistory,
|
||
|
createWebHashHistory,
|
||
|
} from "vue-router";
|
||
|
import { constantRoute, errorRoute } from "./routes";
|
||
|
import { useUserStore } from "@/stores/modules/userStore";
|
||
|
|
||
|
const router = createRouter({
|
||
|
history: createWebHashHistory(),
|
||
|
routes: [...constantRoute, ...errorRoute],
|
||
|
strict: false,
|
||
|
scrollBehavior: () => ({ left: 0, top: 0 }), //滚动行为
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* 前置路由守卫
|
||
|
*/
|
||
|
router.beforeEach((to: any, from: any, next: any) => {
|
||
|
//动态设置标题
|
||
|
const title: string = "后台管理系统";
|
||
|
document.title = to.meta.name ? `${to.meta.name} - ${title}` : title;
|
||
|
//todo 鉴权操作...
|
||
|
const userStore = useUserStore();
|
||
|
if (userStore.token) {
|
||
|
if (to.path === "/login") {
|
||
|
next({ path: "/home" });
|
||
|
} else {
|
||
|
next();
|
||
|
}
|
||
|
} else {
|
||
|
if (to.path === "/login") {
|
||
|
next();
|
||
|
} else {
|
||
|
next({ path: "/login", query: { redirect: to.path } });
|
||
|
}
|
||
|
}
|
||
|
// if (userStore.userInfo?.tokenValue) {
|
||
|
// if (to.path === "/login") {
|
||
|
// next({ path: "/home" });
|
||
|
// } else {
|
||
|
// next();
|
||
|
// }
|
||
|
// } else {
|
||
|
// if (to.path === "/login") {
|
||
|
// next();
|
||
|
// } else {
|
||
|
// next({ path: "/login", query: { redirect: to.path } });
|
||
|
// }
|
||
|
// }
|
||
|
//放行
|
||
|
return next();
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* 路由跳转错误
|
||
|
*/
|
||
|
router.onError((error: any) => {
|
||
|
console.warn("路由错误", error.message);
|
||
|
});
|
||
|
|
||
|
/**
|
||
|
* 后置路由守卫
|
||
|
* */
|
||
|
router.afterEach((to: any, from: any) => {
|
||
|
//...
|
||
|
});
|
||
|
|
||
|
export default router;
|