index.ts 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { createRouter, createWebHistory } from 'vue-router'
  2. import { authLogin } from '@/utils/axios/auth'
  3. import LoginRoutes from './login'
  4. import HomeRoutes from './home'
  5. import Material from './material'
  6. // 路由配置
  7. const routes = [
  8. ...LoginRoutes,
  9. {
  10. path: '/index',
  11. name: 'Index',
  12. component: () => import('@/views/Index.vue'),
  13. children: [...HomeRoutes, ...Material],
  14. },
  15. {
  16. path: '/',
  17. redirect: '/home',
  18. },
  19. {
  20. //访问不存在的路由的时候,跳转到首页
  21. path: '/:pathMatch(.*)',
  22. redirect: '/home',
  23. },
  24. ]
  25. const router = createRouter({
  26. // history: createWebHashHistory(),
  27. history: createWebHistory(),
  28. routes,
  29. })
  30. router.beforeEach((to, _from, next) => {
  31. if (to.name === 'Login') {
  32. next()
  33. } else {
  34. if (authLogin()) {
  35. next()
  36. } else {
  37. router.push('/login')
  38. }
  39. }
  40. })
  41. export default router