2024-03-31 15:51:00 +08:00

61 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { createRouter, createWebHashHistory } from "vue-router";
import { dynamicRoutes, staticRoutes, notFoundAndNoPower } from "@/router/route.ts";
import NProgress from "@/config/nprogress";
/**
* 创建vue的路由示例
* @method createRouter(options: RouterOptions): Router
* @link 参考https://next.router.vuejs.org/zh/api/#createrouter
*/
export const router = createRouter({
history: createWebHashHistory(),
/**
* 说明:
* 1、notFoundAndNoPower 添加默认 404、401界面防止提示 No match found for location with path 'xxx'
* 2、后端控制路由中也需要添加 notFoundAndNoPower 404、401界面
* 防止 404、401 不在 layout 布局中不设置的话404、401界面将全屏显示
*/
routes: [...dynamicRoutes, ...staticRoutes, ...notFoundAndNoPower] // 这里只需要设置兜底路由即可其它的路由通过addRoute动态添加
});
// 路由加载前
router.beforeEach(async (to, from, next) => {
NProgress.start(); // 开启进度条
next();
console.log(to, from);
// if (to.path === "/login" && !sessionStorage.getItem("token")) {
// next();
// } else if (!sessionStorage.getItem("token")) {
// next("/login");
// } else if (to.path === "/login" && sessionStorage.getItem("token")) {
// next("/home");
// } else {
// // 如果进的不是login则判断路由动态添加
// // 获取缓存的路由store
// const stores = useRoutesListStore(pinia);
// const { routesList } = storeToRefs(stores);
// // 如果缓存的路由是0则说明未动态添加路由先添加再跳转
// // 解决刷新页面404的问题
// if (routesList.value.length == 0) {
// await initSetRouter();
// next({ path: to.path, query: to.query });
// } else {
// // 动态路由添加后走这里,直接放行
// next();
// }
// }
});
// 路由跳转错误
router.onError(error => {
NProgress.done();
console.warn("路由错误", error.message);
});
// 路由加载后
router.afterEach(() => {
NProgress.done();
});
export default router;