我们知道,vuex是vue技术栈中很重要的一部分,是一个很好用的状态管理库。

如果你的项目没有那么复杂,或者对vuex的使用没有那么重度,那么,是用不着modules功能的。

但如果你写着写着就发现你的vuex 代码过于臃肿,那么就可能需要modules功能来进行模块化改造了。

由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。

为了解决以上问题,Vuex 允许我们将 store 分割成模块(module)。每个模块拥有自己的 state、mutation、action、getter、甚至是嵌套子模块——从上至下进行同样方式的分割:

——vuex文档

那么,怎么改呢?

以文档的计数器demo为例:

这是demo的vuex代码:

import Vue from 'vue'
import Vuex from 'vuex' Vue.use(Vuex) // root state object.
// each Vuex instance is just a single state tree.
const state = {
count: 0
} // mutations are operations that actually mutates the state.
// each mutation handler gets the entire state tree as the
// first argument, followed by additional payload arguments.
// mutations must be synchronous and can be recorded by plugins
// for debugging purposes.
const mutations = {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
} // actions are functions that cause side effects and can involve
// asynchronous operations.
const actions = {
increment: ({ commit }) => commit('increment'),
decrement: ({ commit }) => commit('decrement'),
incrementIfOdd ({ commit, state }) {
if ((state.count + 1) % 2 === 0) {
commit('increment')
}
},
incrementAsync ({ commit }) {
return new Promise((resolve, reject) => {
setTimeout(() => {
commit('increment')
resolve()
}, 1000)
})
}
} // getters are functions
const getters = {
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd',
originNumber: state => state.count
} // A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default new Vuex.Store({
state,
getters,
actions,
mutations
})

1 新建文件,叫part_a.js

2 store.js的代码复制进去,并修改:

// import Vue from 'vue'
// import Vuex from 'vuex' // Vue.use(Vuex) // root state object.
// each Vuex instance is just a single state tree.
const state = {
count: 0
} // mutations are operations that actually mutates the state.
// each mutation handler gets the entire state tree as the
// first argument, followed by additional payload arguments.
// mutations must be synchronous and can be recorded by plugins
// for debugging purposes.
const mutations = {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
} // actions are functions that cause side effects and can involve
// asynchronous operations.
const actions = {
increment: ({ commit }) => commit('increment'),
decrement: ({ commit }) => commit('decrement'),
incrementIfOdd ({ commit, state }) {
if ((state.count + 1) % 2 === 0) {
commit('increment')
}
},
incrementAsync ({ commit }) {
return new Promise((resolve, reject) => {
setTimeout(() => {
commit('increment')
resolve()
}, 1000)
})
}
} // getters are functions
const getters = {
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd',
originNumber: state => state.count
} // A Vuex instance is created by combining the state, mutations, actions,
// and getters.
export default {
namespaced: true,
state,
getters,
actions,
mutations
}

注意白底的部分:import 和 use 被注释了,输出变成了 export default { },输出对象中加上了 namespaced: true

3 修改store.js

import Vue from 'vue'
import Vuex from 'vuex'
import part_a from './part_a' Vue.use(Vuex) export default new Vuex.Store({
modules: {
part_a
}
})

因为业务逻辑代码放进了part_a.js,所以store.js就不需要保留这一部分。

注意白底的部分:加上了part_a.js的引用,Store()中的对象只有modules对象,里面是引用的part_a。

4 修改*.vue文件

...
computed: {
...mapGetters([
'evenOrOdd',
'originNumber'
]),
},
methods: {
...mapActions([
'increment',
'decrement',
'incrementIfOdd',
'incrementAsync'
])
},
...
...
computed: {
...mapGetters('part_a', [
'evenOrOdd',
'originNumber'
]),
},
methods: {
...mapActions('part_a', [
'increment',
'decrement',
'incrementIfOdd',
'incrementAsync'
])
},
...

注意白底的部分。加一个参数就可以了。

mutations 和 state 可以只有私有调用,所以mapMutations和mapState就不需要了。

以上。

vuex : 模块化改造的更多相关文章

  1. Vuex 模块化与项目实例 (2.0)

    Vuex 强调使用单一状态树,即在一个项目里只有一个 store,这个 store 集中管理了项目中所有的数据以及对数据的操作行为.但是这样带来的问题是 store 可能会非常臃肿庞大不易维护,所以就 ...

  2. node.js入门学习(五)--Demo模块化改造

    1.node.js中模块的分类 1)node.js内置模块(核心,原生) 所有内置模块在安装node.js时就已经编译成二进制文件,可以直接加载运行(速度较快),部分内置模块,在node.exe这个进 ...

  3. Vuex 模块化实现待办事项的状态管理

    前言 在vue里,组件之间的作用域是独立的,父组件跟子组件之间的通讯可以通过prop属性来传参,但是在兄弟组件之间通讯就比较麻烦了.比如A组件要告诉一件事给B组件,那么A就要先告诉他们的爸组件,然后爸 ...

  4. Vuex模块化

    上图是vuex的结构图vuex即 store, 包含State,Action,Mutations, 每一个vue项目都需要使用vuex做组件之间的数据共享 使用场景: 数据最终存放在store的Sta ...

  5. 一个简单的实例演示vuex模块化和命名空间

    因为Vuex Store是全局注册的,不利于较大的项目,引入模块分离业务状态和方法,引入命名空间解决不同模块内(getters,mutaions,actions)名称冲突的问题 ----------- ...

  6. vuex模块化。

    项目结构: 1:在src下新建目录store,然后再建storemodule.js文件,把 上篇 store.js文件抽出来: import Vue from 'vue' import Vuex fr ...

  7. 深入理解Vuex 模块化(module)

    todo https://www.jb51.net/article/124618.htm

  8. vuex数据管理-数据模块化

    对于vue这类mvvm框架来说,其核心就是组件与数据,因此做好相应的数据管理极为重要.这里分享下vuex数据模块化管理的方法,有利于搭建便于维护.协作的vue项目. vuex管理基本方法和使用 模块化 ...

  9. 基于vue2.0+vuex+localStorage开发的本地记事本

    本文采用vue2.0+vuex+localStorage+sass+webpack,实现一个本地存储的记事本.兼容PC端和移动端.在线预览地址:DEMO github地址:https://github ...

随机推荐

  1. 【案例演示】JVM之强引用、软引用、弱引用、虚引用

    1.背景 想要理解对象什么时候回收,就要理解到对象引用这个概念,于是有了下文 2.java中引用对象结构图 3.引用详解 3.1.什么是强引用 a.当内存不足,JVM开始垃圾回收,对于强引用的对象,就 ...

  2. HttpClient优化

    HttpClient优化思路: 1.池化 2.长连接 3.httpclient和httpget复用 4.合理的配置参数(最大并发请求数,各种超时时间,重试次数) 5.异步 6.多读源码 1.背景我们有 ...

  3. elasticserach数据库深度分页查询的原理

    深度分页存在的问题 https://segmentfault.com/a/1190000019004316?utm_source=tag-newest 在实际应用中,分页是必不可少的,例如,前端页面展 ...

  4. layui动态添加的元素click等事件触发不了的解决办法

    在页面加载完成时候 '.add_project' 元素是可以触发click时间的,当动态添加 '.add_project' 时候,新添加的元素却触发不了click事件,类似下面的写法: $(" ...

  5. eclipse导入git项目

    复制项目的git路径 Eclipse打开 Git Repostitories 视图 弹出show view窗口 选择ok ,进入git repositories 视图窗口 我这里已经导入从我的git仓 ...

  6. Centos7 GRE Tunnel

    一.关闭防火墙及selinux 二.CentOS7默认不加载gre内核模块,加载gre内核模块 # modprobe ip_gre   临时加载gre模块(重启后失效) # lsmod |grep g ...

  7. Buy a Ticket 【最短路】

    题目 Musicians of a popular band "Flayer" have announced that they are going to "make t ...

  8. Python-使用tkinter实现的摇骰子小游戏

    贴吧看到的一个求助题,大致需求是:3个人摇骰子,每人摇3次,点数之和最大的获胜,支持玩家名称输入.我觉得这个题目挺有意思的,做了个界面程序,欢迎大家交流指正~ #!usr/bin/env python ...

  9. Linux中清空docker容器日志

    新建文件docker-clear-log,放在/usr/local/bin/目录下,文件内容如下: #!/bin/bash -e if [[ -z $ ]]; then echo "No c ...

  10. 02 . 分布式存储之FastDFS 高可用集群部署

    单节点部署和原理请看上一篇文章 https://www.cnblogs.com/you-men/p/12863555.html 环境 [Fastdfs-Server] 系统 = CentOS7.3 软 ...