index.js 2.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
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'
import Rights from '@/components/menus/MyRights.vue'
import Goods from '@/components/menus/MyGoods.vue'
import Orders from '@/components/menus/MyOrders.vue'
13
import Chat from '@/components/menus/MyChat.vue'
14 15
import Settings from '@/components/menus/MySettings.vue'
import UserDetail from '@/components/user/MyUserDetail.vue'
16
import MyChatDetail from '@/components/user/MyChatDetail.vue'
17
import AddChatDetail from '@/components/user/AddChatDetail.vue'
18

19 20 21 22 23

Vue.use(VueRouter)

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

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

export default router