Vuex的store中的Module

1.单一状态树

什么是单一状态树呢?单一状态树可以简单得理解成整个vuex中只有一个store对象。

这是官方对单一状态树的解释:

Vuex 使用单一状态树——是的,用一个对象就包含了全部的应用层级状态。至此它便作为一个“唯一数据源 (SSOT (opens new window))”而存在。这也意味着,每个应用将仅仅包含一个 store 实例。

单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。

但是当我们全部状态数据都往一个store里面塞的时候,就会出现store过于臃肿,幸好Vuex允许将store分割成模块。单状态树和模块化并不冲突。

**为了反正store的过于臃肿,Vuex允许将store分割成模块。**由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿。这时我们使用模块化,能有效解决store 臃肿的状况。

2.modules

在Vuex中使用modules字段声明模块。

store中能定义的属性在module都能实现,在module中可以像store一样定义:

  • state
  • mutations
  • actions
  • getters
  • modules(modules的镶嵌)
//模块Aconst moduleA = {  state: () => ({ ... }),  mutations: { ... },  actions: { ... },  getters: { ... }}//模块Bconst moduleB = {  state: () => ({ ... }),  mutations: { ... },  actions: { ... }  //modules: {//模块中允许进行模块的镶嵌,但现实中很少用到}}const store = new Vuex.Store({    //通过modules指定模块  modules: {    a: moduleA,    b: moduleB  }})store.state.a // -> moduleA 的状态store.state.b // -> moduleB 的状态

3.模块内部的 mutation 和 getters

在modules中的mutation 和 getters其实和store并没有太大的异样,只不过modules中的getters可以多传入一个参数rootState,用于获取上一层的state。

const moduleA = {  state: {    count: 0  },  mutations: {    increment (state) {      // 这里的 `state` 对象是模块的局部状态(也就是moduleA的state)      state.count++    }  },  getters: {    doubleCount (state) {        // 这里的 `state` 对象是模块的局部状态(也就是moduleA的state)      return state.count * 2    }  }}const store = new Vuex.Store({  state: {    rootcount: 0  },  mutations: {    decrement (state) {      // 这里的 `state` 对象是模块的局部状态(也就是moduleA的state)      state.count--    }  },  modules: {    a: moduleA  }})//-------------在其他组件中使用模块中的state需要指明模块store.state   // -> moduleA 的状态store.state.rootcount // -> store中的rootcountstore.state.a // -> moduleA 的状态store.state.a.count  //moduleA中的count//-------------其他组件中commit mutations时无需指定模块this.$store.commit('increment')this.$store.commit('decrement')//-------------其他组件中使用模块中getters的时也是无需指定模块的$store.getters.doubleCount

特别的,在模块中的getters可以传入三个参数state, getters, rootState,分别对应:

  • state 模块中的state
  • getters 模块中的 getters
  • rootState 上一层模块或者store的state
const moduleA = {  // ...  getters: {    sumWithRootCount (state, getters, rootState) {      return state.count + rootState.count    }  }}

4.模块内部的 action

同样,对于模块内部的 action,局部状态通过 context.state 暴露出来,根节点状态则为 context.rootState

还是引用官方的案例:

const moduleA = {  // ...  actions: {      //这里使用{ state, commit, rootState }来接收context中的三个对象    incrementIfOddOnRootSum ({ state, commit, rootState }) {      if ((state.count + rootState.count) % 2 === 1) {        commit('increment')      }    }  }}//-------------其他组件中dispatch到modular中的actionsthis.$store.dispatch

5.命名空间

默认情况下,模块内部的 action、mutation 和 getter 是注册在全局命名空间的,也意味这我们可以在全局命名空间中直接使用我们的 action、mutation 和 getter。

回顾一下前面的例子:

//-------------其他组件中commit mutations时无需指定模块this.$store.commit('increment')this.$store.commit('decrement')//-------------其他组件中使用模块中getters的时也是无需指定模块的$store.getters.doubleCount//-------------其他组件中dispatch到modular中的actionsthis.$store.dispatch

为什么我们能直接访问到模块内部的 action、mutation 和 getter,那是因为在默认情况下,vuex模块内部的 action、mutation 和 getter 是注册在全局命名空间的,注册在全局命名空间自然能直接通过store来访问了。

为了模块更高的封装性

如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。

当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。

这里还是引用官方的例子:

const store = new Vuex.Store({  modules: {    account: {      namespaced: true,      // 模块内容(module assets)      state: () => ({ ... }), // 模块内的状态已经是嵌套的了,使用 `namespaced` 属性不会对其产生影响      getters: {        isAdmin () { ... } // -> getters['account/isAdmin']      },      actions: {        login () { ... } // -> dispatch('account/login')      },      mutations: {        login () { ... } // -> commit('account/login')      },      // 嵌套模块      modules: {        // 继承父模块的命名空间        myPage: {          state: () => ({ ... }),          getters: {            profile () { ... } // -> getters['account/profile']          }        },        // 进一步嵌套命名空间        posts: {          namespaced: true,          state: () => ({ ... }),          getters: {            popular () { ... } // -> getters['account/posts/popular']          }        }      }    }  }})

官方的例子已经很明确了,如果还是不理解,我下面列出了上面例子的整个命名空间下的getters,actions,mutations(括号中是注册的):

store (空)

store/account

  • getters (isAdmin,profile)
  • actions (login)
  • mutations (login)

store/account/posts

  • getters (popular)
  • actions (空)
  • mutations (空)

解析:

​ 全局命名空间(store)中只定义了modules,所以为空,而模块account使用了namespaced声明了命名空间,所以有命名空间store/account 。

​ 而模块account中的myPage模块没有使用namespaced声明了命名空间,所以myPage模块的getters的profile会注册在上一层的命名空间,也就是store/account命名空间中。

​ 而模块account中的posts模块,使用了namespaced声明了命名空间,使用有命名空间store/account/posts,也正因如此posts模块的getters,actions,mutations被注册到自己的命名空间下。

使用:

commit('xxx')       //没多级路径的,是访问全局命名空间,也就是store中的getters['account/isAdmin']    //访问命名空间account下dispatch('account/login')commit('account/login')getters['account/profile']getters['account/posts/popular']      //访问命名空间account/posts

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文地址:https://blog.csdn.net/qq_43203949/article/details/112061053

相关文章: