自己注册Vuex,注册路由

一.Vuex的手动引入

1.在src下创建src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'

Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    app,
    settings,
    user
  },
  getters
})

export default store

2. 在src/store/getters.js的代码

const getters = {
  sidebar: state => state.app.sidebar,
  device: state => state.app.device,
  token: state => state.user.token,
  avatar: state => state.user.avatar,
  name: state => state.user.name
}
export default getters

3. 在store/module/app.js+settings.js +user.js

 

4.main.js代码

import store from './store'

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

二.router的手动引入

在src/router/index.js

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'


export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  // 组织架构
  {
    path: '/dapartments',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'departments',
      component: () => import('@/views/dapartments/index'),
      meta: { title: '组织架构', icon: 'tree' }
    }]
  },
  {
    path: '/employees',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'employess',
      component: () => import('@/views/employees/index'),
      meta: { title: '员工', icon: 'people' }
    }]
  },
    // 设置 
    {
      path: '/setting',
      component: Layout, 
      children: [{
        path: '', 
        // path两种写法
        // 一种以/
        name: 'setting',
        component: () => import('@/views/setting/index'),
        meta: { title: '公司设置', icon: 'setting' }
      }]
    },
      // 设置 
  {
    path: '/salarys',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'setting',
      component: () => import('@/views/salarys/index'),
      meta: { title: '工资', icon: 'money' }
    }]
  },
  {
    path: '/social',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'setting',
      component: () => import('@/views/social/index'),
      meta: { title: '社保', icon: 'table' }
    }]
  },
  {
    path: '/attendances',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'setting',
      component: () => import('@/views/attendances/index'),
      meta: { title: '考勤', icon: 'skill' }
    }]
  },
  {
    path: '/approvals',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'setting',
      component: () => import('@/views/approvals/index'),
      meta: { title: '审批', icon: 'tree-table' }
    }]
  },
  {
    path: '/permission',
    component: Layout, 
    children: [{
      path: '', 
      // path两种写法
      // 一种以/
      name: 'setting',
      component: () => import('@/views/permission/index'),
      meta: { title: '员工', icon: 'lock' }
    }]
  },
  
  
  
  
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

2.在main.js下

导入:

import router from './router'

挂载:

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值