index.ts 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. const router = createRouter({
  18. history: createWebHistory(import.meta.env.BASE_URL),
  19. routes: [
  20. {
  21. name: 'layout',
  22. component: Layout,
  23. path: '/',
  24. redirect: {
  25. name: 'classify'
  26. },
  27. children: asyncRoutes
  28. },
  29. {
  30. name: 'login',
  31. path: '/login',
  32. component: Login
  33. }
  34. ]
  35. })
  36. router.beforeEach((to, from, next) => {
  37. if (to.name !== 'login') {
  38. const appStore = useAppStore()
  39. appStore.isLogin ? next() : next({ name: 'login' })
  40. return
  41. }
  42. next()
  43. })
  44. export default router