vuex : 模块化改造
我们知道,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 : 模块化改造的更多相关文章
- Vuex 模块化与项目实例 (2.0)
Vuex 强调使用单一状态树,即在一个项目里只有一个 store,这个 store 集中管理了项目中所有的数据以及对数据的操作行为.但是这样带来的问题是 store 可能会非常臃肿庞大不易维护,所以就 ...
- node.js入门学习(五)--Demo模块化改造
1.node.js中模块的分类 1)node.js内置模块(核心,原生) 所有内置模块在安装node.js时就已经编译成二进制文件,可以直接加载运行(速度较快),部分内置模块,在node.exe这个进 ...
- Vuex 模块化实现待办事项的状态管理
前言 在vue里,组件之间的作用域是独立的,父组件跟子组件之间的通讯可以通过prop属性来传参,但是在兄弟组件之间通讯就比较麻烦了.比如A组件要告诉一件事给B组件,那么A就要先告诉他们的爸组件,然后爸 ...
- Vuex模块化
上图是vuex的结构图vuex即 store, 包含State,Action,Mutations, 每一个vue项目都需要使用vuex做组件之间的数据共享 使用场景: 数据最终存放在store的Sta ...
- 一个简单的实例演示vuex模块化和命名空间
因为Vuex Store是全局注册的,不利于较大的项目,引入模块分离业务状态和方法,引入命名空间解决不同模块内(getters,mutaions,actions)名称冲突的问题 ----------- ...
- vuex模块化。
项目结构: 1:在src下新建目录store,然后再建storemodule.js文件,把 上篇 store.js文件抽出来: import Vue from 'vue' import Vuex fr ...
- 深入理解Vuex 模块化(module)
todo https://www.jb51.net/article/124618.htm
- vuex数据管理-数据模块化
对于vue这类mvvm框架来说,其核心就是组件与数据,因此做好相应的数据管理极为重要.这里分享下vuex数据模块化管理的方法,有利于搭建便于维护.协作的vue项目. vuex管理基本方法和使用 模块化 ...
- 基于vue2.0+vuex+localStorage开发的本地记事本
本文采用vue2.0+vuex+localStorage+sass+webpack,实现一个本地存储的记事本.兼容PC端和移动端.在线预览地址:DEMO github地址:https://github ...
随机推荐
- excel如何快速汇总多个类别的总和?
这个需求是一位在当前抗疫一线的朋友提出的,和各位分享一下. 需求情况 因为众所周知的原因,他每天都需要为照顾的小区居民购买.运送生活物资.小区居民通过表单的形式提交自己每日的需求,最终汇总到一张exc ...
- Eureka心跳健康检查机制和Spring boot admin 节点状态一直为DOWN的排查(忽略某一个节点的健康检查)
https://www.jdon.com/springcloud/eureka-health-monitoring.html 运行阶段执行健康检查的目的是为了从Eureka服务器注册表中识别并删除不可 ...
- android KeyEvent事件机制
package im.weiyuan.com.viewutils; import android.content.Intent; import android.os.PersistableBundle ...
- python 2 与python 3区别汇总
python 2 与python 3区别汇总 一.核心类差异1. Python3 对 Unicode 字符的原生支持.Python2 中使用 ASCII 码作为默认编码方式导致 string 有两种类 ...
- vue多个项目公共化组件方案
前言 最近项目需求,需要把两个vue项目多个一样的模块抽成公共化.考虑采用的方案 1.把公共部分独立出来一个项目,npm发布私有包,使用的项目npm install下载(目前下载使用出现配置错误) 存 ...
- CSS选择器使用
今天要对CSS选择器的使用方法做一个全面的总结(几乎全部是从这篇文章摘抄的 https://blog.csdn.net/qq_39241986/article/details/82185697) CS ...
- .NET Core WEB API接口参数模型绑定
.NET Core WEB API 模型绑定方式有以下表格中的几种: 特性 绑定源 [FromHeader] 请求标头 [FromQuery] 请求查询字符串参数 [FromForm] 请求正文中的表 ...
- P1640 [SCOI2010]连续攻击游戏【并查集】
题目描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备 ...
- JavaScript基础对象创建模式之链式调用模式(Chaining Pattern)(029)
链式调用模式允许一个接一个地调用对象的方法.这种模式不考虑保存函数的返回值,所以整个调用可以在同一行内完成: myobj.method1("hello").method2().me ...
- Spring FactoryBean 缓存
相关文章 Spring 整体架构 编译Spring5.2.0源码 Spring-资源加载 Spring 容器的初始化 Spring-AliasRegistry Spring 获取单例流程(一) Spr ...