123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- /// <reference types="@uni-helper/vite-plugin-uni-pages/client" />
- import { pages, subPackages } from "virtual:uni-pages";
- function generateRoutes() {
- const routes = pages.map((page) => {
- const newPath = `/${page.path}`;
- return { ...page, path: newPath };
- });
- if (subPackages && subPackages.length > 0) {
- subPackages.forEach((subPackage) => {
- const subRoutes = subPackage.pages.map((page: any) => {
- const newPath = `/${subPackage.root}/${page.path}`;
- return { ...page, path: newPath };
- });
- routes.push(...subRoutes);
- });
- }
- return routes;
- }
- const router = createRouter({
- routes: generateRoutes(),
- });
- const writePath = ["index", "login"];
- router.beforeEach((to, from, next) => {
- console.log("🚀 beforeEach 守卫触发:", { to, from });
- const { token } = useUserStore();
- if (!writePath.includes(String(to.name)) && !token) {
- uni.showToast({ title: "请先登录", icon: "none" });
- next(false);
- }
- // 继续导航
- next();
- });
- router.afterEach((to, from) => {
- console.log("🎯 afterEach 钩子触发:", { to, from });
- // // 演示:简单的页面切换记录
- // if (to.path) {
- // console.log(`📄 页面切换完成: ${to.path}`);
- // }
- // // 演示:针对 afterEach 演示页面的简单提示
- // if (to.name === "demo-aftereach") {
- // console.log("📊 进入 afterEach 演示页面");
- // setTimeout(() => {
- // showToast("afterEach 钩子已触发!");
- // }, 500);
- // }
- });
- export default router;
|