Skip to content

Vuex

对于 Vuex 其源码也很少,其初始化的过程跟 Vue-router 很像,都是借助于 Vue.mixin()在每一个组件中添加一个 beforeCreate 生命周期函数方法,并通过 parent 向上寻找的方式去获取全局的 store 实例对象。

在 store 中其通过深度遍历 module 的方式将 module 中的 state、mutation、actions、getters 按照不同的方式存储到根 store 实例对象对应的属性中,其中对于 state 仍然是按照树形的结构进行存储,当时对于 mutation、actions、getters 其就通过扁平化的方式将树结构转换成对应的 namespace 字符串格式存储在 store 中。

  • getter => store._wrappedGetters["moduleA/modueleAB/modueleABA/getters"] = () => {}
  • mutations => store._mutations["moduleA/modueleAB/modueleABA/mutationXXX"] = () => {}
  • actions => store._actions["moduleA/modueleAB/modueleABA/actionXXX"] = () => {}