在Vue-cli 3.X环境下,基于同一类型的活动,能够多个页面复用,大部分组件能够公用的背景
git cherry-pick <commit id>
进行平行迁移。仅在同一分钟下进行多项目的维护,各个功能模块解构,经过项目配置项进行个性化配置。前端
|-src |- views |- index.js // 通用页面的统一入口 |- Company |- index.vue // 通用页面Company结构、样式、逻辑 |- index.js // Company页面路由 |- Rule |- index.vue |- index.js |- components |- core |- instance // 和app实例挂钩的方法 |- libs // 和app实例无关的方法 |- assets |- images |- fonts |- store |- index.js // 通用状态 |- types.js // 事件类型 |- config |- proA.js // 项目资源配置 |- proB.js |- projects // 项目定制资源 |- proA |- proB
不一样项目的区别彻底在于config/
文件的配置和projects/
下的项目定义;同级其他目录是各个项目通用的内容。vue
// src/views/Company/index.vue <template> ... </template> <script> ... </script> <style scoped> ... </style>
// src/views/Company/index.js export default [ { path: '/company', name: 'company', component: () => import(/* webpackChunkName: "company" */ './index.vue'), meta: { title: '公司简介' } } ]
// src/views/index.js export { default as companyRoute } from './Company/index.js' export { default as ruleRoute } from './Rule/index.js'
// src/config/proA.js import { companyRoute, ruleRoute } from '../views/index.js' ... export const logoUrl = '' // 还能够定制其它的内容 export const routes = [ ...companyRoute, ...ruleRoute ]
以src/projects/proA
为例:webpack
目录结构
|- assets |- components |- mixins |- router |- store |- pages |- App.vue |- main.js
项目主路由
// src/projects/proA/router/index.js import Vue from 'vue' import Router from 'vue-router' import { routes } from '../../config/proA' import Home from '../pages/Home' Vue.use(Router) export default new Router({ routes: [ { path: '/', redirect: '/home' }, { path: '/home', name: 'Home', component: Home, meta: { title: '' } }, ...routes ] })
其中:Home/index.vue是定制化的。git
多项目是独立运行时,状态提取不会互相干扰,若一次性运行多个项目,通用状态会被修改。
// src/store/index.js import types from './types' export const initialState = { userInfo: {}, ... } export function getGetters (store) { return { userId: () => store.userInfo.userID, ... } } export function getMutations (store) { return { [types.INITIALMUTATIONTYPES.USER_INFO] (val) { store.userInfo = val }, ... } }
在config/proA.js
文件中追加:web
... export * from '../store/index.js' export * from '../store/types.js' ...
小型项目,使用
vue.observable
管理状态
// src/projects/proA/store/index.js import vue from 'vue' import { initialState, getGetters, getMutations } from '../../../config/proA' export const store = vue.observable({ ...initialState, customState: '', // 项目私有状态 ... }) store._getters = { ...getGetters(store), customGetter() { // 项目私有 return store.customState }, ... } store._mutations = { ...getMutation(store), ... // 项目私有 } export const mutation = { ...getMutations(store), ... // 项目私有 }
mapGetters
拷贝vuex
部分代码到src/core/libs/helpers.js
文件中
export const mapGetters = (getters) => { const res = {} if (!isValidMap(getters)) { console.error('[vuex] mapGetters: mapper parameter must be either an Array or an Object') } normalizeMap(getters).forEach(({ key, val }) => { res[key] = function mappedGetter () { if (!(val in this.$store._getters)) { console.error(`[vuex] unknown getter: ${val}`) return } return this.$store._getters[val]() } }) return res } export function normalizeMap (map) { if (!isValidMap(map)) { return [] } return Array.isArray(map) ? map.map(key => ({ key, val: key })) : Object.keys(map).map(key => ({ key, val: map[key] })) } export function isValidMap (map) { return Array.isArray(map) || isObject(map) } export function isObject (obj) { return obj !== null && typeof obj === 'object' }
在/src/core/libs/index.js
中追加:vue-router
export * from './helpers'
*.vue
中使用// src/projects/proA/pages/Home/index.vue <script> ... import { mapGetters } from '../../../core/libs/' export default { data () { return { ... } }, computed: { ...mapGetters([ 'userId' ]), ... } ... </script>