index.js 2.7 KB
Newer Older
1 2 3 4 5 6 7 8
import Vue from 'vue'
import VueRouter from 'vue-router'
import pathArr from '@/router/pathArr.js'

// 导入需要的组件
import Login from '@/components/MyLogin.vue'
import Home from '@/components/MyHome.vue'

9
import Pic from '@/components/menus/MyPic.vue'
10
import Chat from '@/components/menus/MyChat.vue'
11
import Interview from '@/components/menus/MyInterview.vue'
12
import Algorithmic from '@/components/menus/MyAlgorithmic.vue'
13
import AphorismPoetry from '@/components/menus/MyAphorismPoetry.vue'
14
import MyCsdnUser from '@/components/menus/MyCsdnUser.vue'
15
import MyChatDetail from '@/components/user/MyChatDetail.vue'
16
import AddChatDetail from '@/components/user/AddChatDetail.vue'
17

18 19 20 21 22

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
23 24 25 26
    {
      path: '/',
      redirect: '/login'
    },
27
    // 登录的路由规则
28 29
    {
      path: '/login',
30 31 32 33
      component: Login,
      meta: {
        title: '登录' // 设置默认标题
      }
34
    },
35 36 37 38
    // 后台主页的路由规则
    {
      path: '/home',
      component: Home,
39
      redirect: '/home/interview',
40
      children: [
41
        {
42
          path: 'interview',
43 44 45 46
          component: Interview,
          meta: {
            title: '面试题' // 设置默认标题
          }
47 48
        },
        {
49
          path: 'algorithmic',
50 51 52 53
          component: Algorithmic,
          meta: {
            title: '算法题' // 设置默认标题
          }
54 55
        },
        {
56
          path: 'aphorismpoetry',
57 58 59 60
          component: AphorismPoetry,
          meta: {
            title: '名言警句' // 设置默认标题
          }
61 62
        },
        {
63
          path: 'chat',
64 65 66 67 68
          component: Chat,
          meta: {
            title: 'chatgpt记录' // 设置默认标题
          }
        }, {
69
          path: 'pic',
70 71 72 73 74
          component: Pic,
          meta: {
            title: '图片' // 设置默认标题
          }
        }, {
75 76
          path: 'csdnUser',
          component: MyCsdnUser,
77 78 79 80
          meta: {
            title: '用户管理' // 设置默认标题
          }
        },
81 82 83
        {
          path: 'chatinfo/:id',
          component: MyChatDetail,
84 85 86 87
          props: true,
          meta: {
            title: 'chat信息' // 设置默认标题
          }
88 89
        }, {
          path: 'addChat',
90 91 92 93
          component: AddChatDetail,
          meta: {
            title: '添加chat信息' // 设置默认标题
          }
94
        },
95 96 97 98 99 100
      ]
    }
  ]
})

// 全局前置守卫
101
router.beforeEach(function (to, from, next) {
102 103 104 105 106 107 108 109 110 111 112 113 114
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router