我们知道,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. Newtonsoft 六个超简单又实用的特性,值得一试 【上篇】

    一:讲故事 看完官方文档,阅读了一些 Newtonsoft 源码,对它有了新的认识,先总结 六个超经典又实用的特性,同大家一起分享,废话不多说,快来一起看看吧~~~ 二:特性分析 1. 代码格式化 如 ...

  2. 使用IDEA 发布项目搭配远程仓库 Gitee

    本次讲解的是idea 发布到gitee上 一样的操作流程 没有基础的请先去学习 附上我的 gitee 地址 有资源会发布到gitee 俗话说关注走一走 活到999 https://gitee.com/ ...

  3. 001_Linux常用命令之ls命令

    1. 认识Linux系统目录结构 /bin 可执行文件所在目录 /media 挂载设备媒体,u盘,光驱等 /mnt 该目录主要是为了让用户挂在别的文件系统(挂在自己的u盘) /usr unix sys ...

  4. 《UNIX环境高级编程》(APUE) 笔记第八章 - 进程控制

    8 - 进程控制 Github 地址 1. 进程标识 每个进程都有一个非负整型表示的 唯一进程 ID .进程 ID 是可复用的(延迟复用算法). ID 为 \(0\) 的进程通常是调度进程,常常被称为 ...

  5. js统计字符

    问题:    var str1 = "abcdabcabcaabeeeeeee";     var str2 = "fhjdiovjdasklgudsaklfgdaskl ...

  6. 聊聊Java中的异常及处理

    前言 在编程中异常报错是不可避免的.特别是在学习某个语言初期,看到异常报错就抓耳挠腮,常常开玩笑说编程1分钟,改bug1小时.今天就让我们来看看什么是异常和怎么合理的处理异常吧! 异常与error介绍 ...

  7. springboot集成springDataJpa

    1.引用依赖 <!--spring-data-jpa--> <dependency> <groupId>org.springframework.boot</g ...

  8. VMware实现宿主机和虚拟机处于同一网段

    打开虚拟网络编辑器 选择VMnet0桥接模式,在VMnet信息中,选择可以选择的网卡,然后保存. 打开虚拟机设置,在“硬件”选项卡的网络适配器中选择桥接模式即可.

  9. NameNode是如何存储元数据的?

    1.NN的作用 保存HDFS上所有文件的元数据! 接受客户端的请求! 接受DN上报的信息,给DN分配任务(维护副本数)! 2.元数据的存储 元数据存储在fsiamge文件+edits文件中! fsim ...

  10. 攻防世界FlatScience

    访问robots.txt发现 admin.php和login.php 在admin.php和login.php分别尝试注入 发现login.php页面存在注入,并且根据报错得知数据库类型为sqlite ...