index.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type { RouteRecordRaw } from 'vue-router'
  2. import { createRouter, createWebHistory } from 'vue-router'
  3. import { useAppStore } from '@/stores/app'
  4. import Login from '@/pages/common/login.vue'
  5. import Layout from '@/layouts/Layout.vue'
  6. export const asyncRoutes: RouteRecordRaw[] = [
  7. {
  8. name: 'category',
  9. path: 'category',
  10. component: () => import('@/pages/category/index.vue'),
  11. meta: {
  12. title: '分类管理',
  13. icon: 'system-3'
  14. }
  15. },
  16. {
  17. name: 'article',
  18. path: 'article',
  19. component: () => import('@/pages/article/index.vue'),
  20. meta: {
  21. title: '咨询管理',
  22. icon: 'collection'
  23. }
  24. },
  25. {
  26. name: 'carousal',
  27. path: 'carousal',
  28. component: () => import('@/pages/carousal/index.vue'),
  29. meta: {
  30. title: '轮播图管理',
  31. icon: 'image'
  32. }
  33. },
  34. {
  35. name: 'product',
  36. path: 'product',
  37. meta: {
  38. title: '产品管理',
  39. icon: 'shop'
  40. },
  41. component: () => import('@/pages/product/index.vue')
  42. },
  43. {
  44. name: 'pet-variety',
  45. path: 'pet-variety',
  46. meta: {
  47. title: '宠物品种',
  48. icon: 'cat'
  49. },
  50. component: () => import('@/pages/pet-variety/index.vue')
  51. }
  52. ]
  53. const router = createRouter({
  54. history: createWebHistory(import.meta.env.BASE_URL),
  55. routes: [
  56. {
  57. name: 'layout',
  58. component: Layout,
  59. path: '/',
  60. redirect: {
  61. name: 'category'
  62. },
  63. children: asyncRoutes
  64. },
  65. {
  66. name: 'login',
  67. path: '/login',
  68. component: Login
  69. }
  70. ]
  71. })
  72. router.beforeEach((to, from, next) => {
  73. if (to.name !== 'login') {
  74. const appStore = useAppStore()
  75. appStore.isLogin ? next() : next({ name: 'login' })
  76. return
  77. }
  78. next()
  79. })
  80. export default router