123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import {
- createRouter,
- createWebHashHistory,
- type RouteLocationNormalized
- } from "vue-router";
- import { constantRoutes } from "./routes";
- import { useCachedViewStoreHook } from "@/store/modules/cachedView";
- import NProgress from "@/utils/progress";
- import setPageTitle from "@/utils/set-page-title";
- import useUserStore from "@/store/modules/user";
- let router = createRouter({
- history: createWebHashHistory(),
- routes: constantRoutes
- });
- export interface toRouteType extends RouteLocationNormalized {
- meta: {
- title?: string;
- noCache?: boolean;
- };
- }
- // const whiteList = ['/roleSelect'];
- router.beforeEach((to: toRouteType, from, next) => {
- NProgress.start();
- // 路由缓存
- useCachedViewStoreHook().addCachedView(to);
- // 页面 title
- setPageTitle(to.meta.title);
- const role = localStorage.getItem('role');
- const roles = useUserStore().roles;
- if(to.path === '/' || (!!role && roles.includes(role))) {
- next();
- } else if (!!role) {
- useUserStore().setRoles(role);
- next({ path: to.path, replace: true, params: to.params, query: to.query, hash: to.hash, name: to.name as string }); // hack方法 确保addRoutes已完成
- } else {
- next({ path: '/' });
- }
- });
- router.afterEach(() => {
- NProgress.done();
- });
- export default router;
|