index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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: 'classify',
  9. path: 'classify',
  10. component: () => import('@/pages/classify/index.vue'),
  11. meta: {
  12. title: '分类管理',
  13. icon: 'system-3'
  14. }
  15. },
  16. {
  17. name: 'carousal',
  18. path: 'carousal',
  19. component: () => import('@/pages/carousal/index.vue'),
  20. meta: {
  21. title: '轮播图管理',
  22. icon: 'system-3'
  23. }
  24. },
  25. ]
  26. const router = createRouter({
  27. history: createWebHistory(import.meta.env.BASE_URL),
  28. routes: [
  29. {
  30. name: 'layout',
  31. component: Layout,
  32. path: '/',
  33. redirect: {
  34. name: 'classify'
  35. },
  36. children: asyncRoutes
  37. },
  38. {
  39. name: 'login',
  40. path: '/login',
  41. component: Login
  42. }
  43. ]
  44. })
  45. router.beforeEach((to, from, next) => {
  46. if (to.name !== 'login') {
  47. const appStore = useAppStore()
  48. appStore.isLogin ? next() : next({ name: 'login' })
  49. return
  50. }
  51. next()
  52. })
  53. export default router