1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| import { createRouter, createWebHashHistory } from "uni-simple-router";
const routes = [ { path: "/", redirect: "/pages/index/index", }, { path: "/pages/index/index", name: "Home", component: () => import("@/pages/index/index.vue"), meta: { title: "首页", requiresAuth: false, }, }, { path: "/pages/user/index", name: "User", component: () => import("@/pages/user/index.vue"), meta: { title: "用户中心", requiresAuth: true, }, }, { path: "/pages/detail/:id", name: "Detail", component: () => import("@/pages/detail/index.vue"), meta: { title: "详情页", }, }, { path: "/:pathMatch(.*)*", name: "NotFound", component: () => import("@/pages/404.vue"), }, ];
const router = createRouter({ history: createWebHashHistory(), routes, });
router.beforeEach((to, from, next) => { if (to.meta.requiresAuth && !uni.getStorageSync("token")) { return next({ path: "/pages/login/index", query: { redirect: to.fullPath }, }); } if (to.meta.title) { uni.setNavigationBarTitle({ title: to.meta.title }); } next(); });
export default router;
|