vuex : 模块化改造

摘要:
importVuefrom'vu'importVuexfrom'vuex'vue.use(vuex)//rootstateobject.//每个Vuexinstanceisjustinglesatetree.conststate={count:

我们知道,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
}

注意白底的部分:importuse 被注释了,输出变成了 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 : 模块化改造》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇oracle最高账号sys的密码认证模式老技术记录-C#+SqlServer使用SqlDependency监听数据库表变化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

二十、异常捕获及处理详解

代码中被[]包含的表示可选,|符号分开的表示可选其一。 需求背景 我们在写存储过程的时候,可能会出现下列一些情况: 插入的数据违反唯一约束,导致插入失败 插入或者更新数据超过字段最大长度,导致操作失败 update影响行数和期望结果不一致 遇到上面各种异常情况的时,可能需要我们能够捕获,然后可能需要回滚当前事务。 本文主要围绕异常处理这块做详细的介绍。...

c语言_头文件_windows.h

概述 Win32程序的开头都可看到: #include <windows.h> WINDOWS.H是一个最重要的头文件,它包含了其他Windows头文件,这些头文件的某些也包含了其他头文件。这些头文件中最重要的和最基本的是: WINDEF.H 基本数据类型定义。 WINNT.H 支持Unicode的类型定义。 WINBASE.H Kernel(...

python学习笔记(九)内置函数

1 print(all([1,2,3,4]))#判断可迭代的对象里面的值是否都为真 True 2 print(any([0,1,2,3,4]))#判断可迭代的对象里面的值是否有一个为真 True 3 4 print(bin(10))#十进制转二进制 5 ejz=bin(100) #0b1010 6 print(ejz.replace('0...

Oracle中索引的使用 索引性能优化调整

索引是由Oracle维护的可选结构,为数据提供快速的访问。准确地判断在什么地方需要使用索引是困难的,使用索引有利于调节检索速度。 当建立一个索引时,必须指定用于跟踪的表名以及一个或多个表列。一旦建立了索引,在用户表中建立、更改和删除数据库时, Oracle就自动地维护索引。创建索引时,下列准则将帮助用户做出决定:        1) 索引应该在SQL语句的...

【自制插件】将MMD4Mecanim转换的MMD模型导入maya

这个已经废弃了_(:зゝ∠)_,另外做了升级版: http://www.cnblogs.com/marisa/p/5174150.html ======================================================================== 这个东西是给想把MMD模型导入maya或者其他3D软件或者游戏引擎的童鞋...

highcharts图表

<!--图表例子--> <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Highcharts Example...