router.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { App } from "vue";
  2. import { createRouter, createWebHistory } from "vue-router";
  3. import { token } from "./pinia";
  4. const router = createRouter({
  5. history: createWebHistory(),
  6. routes: [
  7. {
  8. path: "/",
  9. component: () => import("@/layouts/home.vue"),
  10. beforeEnter: (to, from, next) => {
  11. if (token().token) {
  12. next();
  13. } else {
  14. next("/login");
  15. }
  16. },
  17. redirect: "/home",
  18. children: [
  19. {
  20. path: "/home",
  21. component: () => import("@/layouts/index.vue"),
  22. },
  23. {
  24. path: "/user",
  25. component: () => import("@/layouts/user.vue"),
  26. },
  27. {
  28. path: "/comments",
  29. component: () => import("@/layouts/comments.vue"),
  30. },
  31. {
  32. path: "/plate",
  33. component: () => import("@/layouts/plate.vue"),
  34. },
  35. {
  36. path: "/post",
  37. component: () => import("@/layouts/post.vue"),
  38. children: [],
  39. },
  40. {
  41. path: "/newpost",
  42. component: () => import("@/components/post/newpost.vue"),
  43. },
  44. ],
  45. },
  46. {
  47. path: "/login",
  48. component: () => import("@/layouts/login.vue"),
  49. beforeEnter: (to, from, next) => {
  50. if (!token().token) {
  51. next();
  52. } else {
  53. next("/home");
  54. }
  55. },
  56. },
  57. ],
  58. });
  59. export function setupRouter(app: App<Element>) {
  60. app.use(router);
  61. }
  62. export default router;