自己注册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自己注册Vuex,注册路由_第1张图片

 

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)
})

你可能感兴趣的:(vue.js,前端,javascript)