index.js 2.2 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 12
import Goods from '@/components/menus/MyGoods.vue'
import Orders from '@/components/menus/MyOrders.vue'
13
import Chat from '@/components/menus/MyChat.vue'
14
import Interview from '@/components/menus/MyInterview.vue'
15 16
import Settings from '@/components/menus/MySettings.vue'
import UserDetail from '@/components/user/MyUserDetail.vue'
17
import MyChatDetail from '@/components/user/MyChatDetail.vue'
18
import AddChatDetail from '@/components/user/AddChatDetail.vue'
19

20 21 22 23 24

Vue.use(VueRouter)

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

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

export default router