index.js 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
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'

import Users from '@/components/menus/MyUsers.vue'
10
import Pic from '@/components/menus/MyPic.vue'
11
import Orders from '@/components/menus/MyOrders.vue'
12
import Chat from '@/components/menus/MyChat.vue'
13
import Interview from '@/components/menus/MyInterview.vue'
14
import Algorithmic from '@/components/menus/MyAlgorithmic.vue'
15
import AphorismPoetry from '@/components/menus/MyAphorismPoetry.vue'
16 17
import Settings from '@/components/menus/MySettings.vue'
import UserDetail from '@/components/user/MyUserDetail.vue'
18
import MyChatDetail from '@/components/user/MyChatDetail.vue'
19
import AddChatDetail from '@/components/user/AddChatDetail.vue'
20

21 22 23 24 25

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
26 27 28 29
    {
      path: '/',
      redirect: '/login'
    },
30
    // 登录的路由规则
31 32 33 34
    {
      path: '/login',
      component: Login
    },
35 36 37 38
    // 后台主页的路由规则
    {
      path: '/home',
      component: Home,
39
      redirect: '/home/interview',
40
      children: [
41 42 43 44 45
        {
          path: 'users',
          component: Users
        },
        {
46 47
          path: 'pic',
          component: Pic
48 49
        },
        {
50 51
          path: 'algorithmic',
          component: Algorithmic
52 53
        },
        {
54 55 56
          path: 'aphorismpoetry',
          component: AphorismPoetry
        }, {
57 58 59 60 61
          path: 'orders',
          component: Orders
        }, {
          path: 'chat',
          component: Chat
62 63 64
        }, {
          path: 'interview',
          component: Interview
65 66 67 68
        }, {
          path: 'settings',
          component: Settings
        },
69
        // 用户详情页的路由规则
70 71 72 73 74 75 76 77 78 79
        {
          path: 'userinfo/:id',
          component: UserDetail,
          props: true
        }, {
          path: 'chatinfo/:id',
          component: MyChatDetail,
          props: true
        }, {
          path: 'addChat',
80
          component: AddChatDetail
81
        },
82 83 84 85 86 87
      ]
    }
  ]
})

// 全局前置守卫
88
router.beforeEach(function (to, from, next) {
89 90 91 92 93 94 95 96 97 98 99 100 101
  if (pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router