关于vue.js:聊聊Vuex原理

4次阅读

共计 11347 个字符,预计需要花费 29 分钟才能阅读完成。

背景

Vuex 是一个专为 Vue.js 利用程序开发的 状态管理模式。Vuex 是专门为 Vue.js 设计的状态治理库,以利用 Vue.js 的细粒度数据响应机制来进行高效的状态更新。如果你曾经灵活运用,然而仍然好奇它底层实现逻辑,无妨一探到底。

Vue 组件开发

咱们晓得开发 Vue 插件,装置的时候须要执行 Vue.use(Vuex)

import Vue from 'vue'
import Vuex from '../vuex'

Vue.use(Vuex)

通过查看 Vue API Vue-use 开发文档,咱们晓得装置 Vue.js 插件。如果插件是一个对象,必须提供 install 办法。如果插件是一个函数,它会被作为 install 办法。install 办法调用时,会将 Vue 作为参数传入。该办法须要在调用 new Vue() 之前被调用。当 install 办法被同一个插件屡次调用,插件将只会被装置一次。

为了更好了的去了解源码意思,这里写了一个简略的测试实例。

测试实例代码

import Vue from 'vue'
import Vuex from '../vuex'

Vue.use(Vuex)

export default new Vuex.Store({plugins: [],
  state: {
    time: 1,
    userInfo: {
      avatar: '',
      account_name: '',
      name: ''
    },
  },
  getters: {getTime (state) {console.log('1212',state)
      return state.time
    }
  },
  mutations: {updateTime(state, payload){state.time = payload}
  },
  actions: {operateGrou({ commit}) {// commit('updateTime', 100)
      return Promise.resolve().then(()=>{
        return {rows: [1,2,3]
        }
      })
    }
  },
  modules: {
    report: {
      namespaced: true,
      state: {title: '',},
      getters: {getTitle (state) {return state.title}
      },
      mutations: {updateTitle(state, payload){state.title = payload}
      },
      actions: {operateGrou({ commit}) {commit('updateTitle', 100)
          return Promise.resolve().then(()=>{
            return {rows: [1,2,2,3]
            }
          })
        }
      },
      modules: {
        reportChild: {
          namespaced: true,
          state: {titleChild: '',},
          mutations: {updateTitle(state, payload){state.title = payload}
          },
          actions: {operateGrou({ commit}) {commit('updateTitle', 100)
              return Promise.resolve().then(()=>{
                return {rows: [1,2,2,3]
                }
              })
            }
          },
        }
      }
    },
    part: {
      namespaced: true,
      state: {title: '',},
      mutations: {updateTitle(state, payload){state.title = payload},
        updateTitle1(state, payload){state.title = payload}
      },
      actions: {operateGrou({ commit}) {commit('updateTitle', 100)
          return Promise.resolve().then(()=>{
            return {rows: [1,2,2,3]
            }
          })
        }
      },
      modules: {
        partChild: {
          namespaced: true,
          state: {titleChild: '',},
          getters: {getTitleChild (state) {return state.titleChild}
          },
          mutations: {updateTitle(state, payload){state.titleChild = payload}
          },
          actions: {operateGrou({ commit}) {commit('updateTitle', 1000)
              return Promise.resolve().then(()=>{
                return {rows: [1,2,2,3]
                }
              })
            }
          },
          modules: {
            partChildChild: {
              namespaced: true,
              state: {titleChild: '',},
              getters: {getTitleChild (state) {return state.titleChild}
              },
              mutations: {updateTitle(state, payload){state.titleChild = payload}
              },
              actions: {operateGrou({ commit}) {commit('updateTitle', 1000)
                  return Promise.resolve().then(()=>{
                    return {rows: [1,2,2,3]
                    }
                  })
                }
              },
            }
          }
        }
      }
    }
  }
})

Graphviz 父子结点关系图

用 Graphviz 图来示意一下父子节点的关系,不便了解

组件开发第一步 install & mixin

在调用 Vuex 的时候会找其 install 办法,并把组件实例传递到 install 办法的参数中。

let Vue;
class Store {

}

const install = _Vue => {
    Vue = _Vue;
    Vue.mixin({beforeCreate(){console.log(this.$options.name);
        }
    })
};

export default {
    Store,
    install
}

到这里说一下 Vuex 实现的思维,在 Vuex 的 install 办法中,能够获取到 Vue 实例。
咱们在每个 Vue 实例上增加 $store 属性,能够让每个属性拜访到 Vuex 数据信息;
咱们在每个 Vue 实例的 data 属性上增加上 state,这样 state 就是响应式的;
收集咱们传入 new Vuex.Store(options) 即 options 中所有的 mutaions、actions、getters;
接着当咱们 dispatch 的时候去匹配到 Store 类中寄存的 actions 办法,而后去执行;
当咱们 commit 的时候去匹配到 Store 类中寄存的 mutations 办法,而后去执行;
这其实就是一个公布订阅模式,先存起来,后边用到再取再执行。好理解这些,咱们开始真正的源码剖析;参考 vue 实战视频解说:进入学习

Vue 实例注入 $store

为了更好了解,咱们打印出 Vue 实例,能够看到注入了 $store,见下图。

具体实现关键点

const install = (_Vue) => {
  Vue = _Vue
  Vue.mixin({beforeCreate(){
      // 咱们能够看上面 main.js 默认只有咱们的根实例上有 store,故 this.$options.store 有值是根结点
      if(this.$options.store) {this.$store = this.$options.store // 根结点赋值} else {this.$store = this.$parent && this.$parent.$store // 每个实例都会有父亲。故一层层给实例赋值}
    }
  })
}
  • main.js
import Vue from 'vue'
import App from './App.vue'
import store from './store'

Vue.config.productionTip = false

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

$store.state 响应式

响应式外围就是挂载到实例 data 上,让 Vue 外部使用 Object.defineProperty 实现响应式。

class Store{constructor (options) {// 咱们晓得 options 是用户传入 new Vuex.Store(options) 参数
    this.vm = new Vue({
      data: {state: options.state}
    })
  }
}

$store.mutations & commit

来看下 用户传入的 mutations 变成了什么,数据采纳 最下面的测试实例代码。
咱们能够看到 mutations 是一个对象,外面放了函数名,值是数组,将雷同函数名对应的函数寄存到数组中。

mutations

  • 实际上就是收集用户传入的 mutations, 放到一个对象中。
 const setMoutations = (data, path = []) => {
    const mutations = data.mutations
    Object.keys(mutations).map(item => {this.mutations[item] = this.mutations[item] || [] // 之前的旧值
      this.mutations[item].push(mutations[item]) // 存起来
    })
    const otherModules = data.modules || {} // 有子 modules 则递归
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setMoutations(otherModules[item], path.concat(item))
      })
    }
  }
  setMoutations(options) // 这里 options 是用户传入的 new Vuex.Store(options) 的参数

commit

  • 实际上就是从收集 mutaitons 中找到用户传入的 mutationName 对应的数组办法,而后遍历执行。告诉到位。
class Store{commit = (mutationName, payload) => {this.mutations[mutationName].map(fn => {fn(this.state, payload)
    })
  }
}

$store.actions & dispatch

actions

  • actions 与 mutations 实现是一样的
  const setAction = (data, path = []) => {
    const actions = data.actions
    Object.keys(actions).map(item => {this.actions[item] = this.actions[item] || []
      this.actions[item].push(actions[item])
    })
    const otherModules = data.modules || {}
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setAction(otherModules[item], path.concat(item))
      })
    }
  }
  setAction(options)

dispatch

class Store{dispatch = (acitonName, payload) => {this.actions[acitonName].map(fn => {fn(this, payload) // this.$store.dispatch('operateGrou')
    })
  }
}

$store.getters

const setGetter = (data, path = []) => {const getter = data.getters || {}
  const namespace = data.namespaced
  Object.keys(getter).map(item => {     
    // 跟 Vue 计算属性底层实现相似,当从 store.getters.doneTodos 取值的时候,理论会执行 这个办法。Object.defineProperty(this.getter, item, {get:() => {return options.state.getters[item](this.state)
      }
    })
  })

  const otherModules = data.modules || {}
  if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setGetter(otherModules[item], path.concat(item))
    })
  }
}
setGetter(options)

namespaced

下面探讨的是没有 namespaced 的状况,加上 namespaced 有什么区别呢,见下图。

霎时拨云见日了,平时写下面基本上都要加上 namespaced,避免命名抵触,办法反复屡次执行。
当初就算每个 modules 的办法命一样,也默认回加上这个办法别突围的所有父结点的 key。
上面对 mutations actions getters 扩大一下,让他们反对 namespaced。外围就是 path 变量

mutations

// 外围点在 path
const setMoutations = (data, path = []) => {
    const mutations = data.mutations
    const namespace = data.namespaced
    Object.keys(mutations).map(item => {
      let key = item
      if (namespace) {key = path.join('/').concat('/'+item) // 将所有父亲用 斜杠 相关联
      }
      this.mutations[key] = this.mutations[key] || []
      this.mutations[key].push(mutations[item])
    })
    const otherModules = data.modules || {}
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setMoutations(otherModules[item], path.concat(item)) // path.concat 不会批改 path 原来的值
      })
    }
  }
  setMoutations(options)

actions

actions 与 mutations 是一样的

const setAction = (data, path = []) => {
    const actions = data.actions
    const namespace = data.namespaced
    Object.keys(actions).map(item => {
      let key = item
      if (namespace) {key = path.join('/').concat('/'+item)
      }
      this.actions[key] = this.actions[key] || []
      // this.actions[key].push(actions[item]) 
      this.actions[key].push((payload) => {actions[item](this, payload);
      })
    })
    const otherModules = data.modules || {}
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setAction(otherModules[item], path.concat(item))
      })
    }
  }
  setAction(options)

getter

 const setGetter = (data, path = []) => {const getter = data.getters || {}
    const namespace = data.namespaced
    Object.keys(getter).map(item => {
      let key = item
      if (namespace) {key = path.join('/').concat('/'+item)
      }
      Object.defineProperty(this.getter, key, {get: () => {return getter[item](this.state)
        }
      })
    })
    const otherModules = data.modules || {}
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setGetter(otherModules[item], path.concat(item))
      })
    }
  }
  setGetter(options)

咱们能够总结来看,namespaces 加与不加的区别理论就下图;

具体数据转化形式有很多,外围就是对数据格式的解决,来进行公布与订阅;

actions & mutations

看到这,小伙伴狐疑了,actions 与 mutations,具体实现是一样的,那为什么要说 actions 能够异步执行,mutations,不能异步执行呢?上面我来贴一下外围代码。

class Store{constructor () {
    ////..... 省略

      if(this.strict){// 严格模式下才给报错提醒
      this.vm.$watch(()=>{return this.vm.state // 咱们晓得 commit 是会登程 state 值批改的},function () {
            // 此处监听 state 批改,因为在执行 commit 的时候 this._committing 是 true 的,你若放了异步办法,this._committing 就会往下执行 变成 false
          console.assert(this._committing,'您异步调用了!')  // 断言  this._committing 为 false, 给报错提醒
      },{deep:true,sync:true});
    }
  }
  _withCommit(fn){
    const committing = this._committing; // 保留 false
    this._committing = true; // 调用 mutation 之前, this._committing 更改值是 true
    fn(); // 保障 执行的时候 this._committing 是 true
    this._committing = committing // 完结后重置为 false
  }
  commit = (mutationName, payload) => {console.log('1212',mutationName)
    this._withCommit(()=>{this.mutations[mutationName] && this.mutations[mutationName].map(fn => {fn(this.state, payload)
      })
    })
  }
}

Vuex 中的辅助办法

咱们常常在 Vuex 中这样应用

import {
  mapState,
  mapGetters
} from 'vuex'

computed: {isAfterSale () {return this.$route.meta.isAfterSale},
  ...mapGetters({messageState: 'message/getMessageState'}),
  ...mapGetters({messageNum: 'message/getMessageNum'}),
 ...mapGetters(['doneTodosCount',    'anotherGetter',    // ...])
},

 methods: {...mapActions([    'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`    // `mapActions` 也反对载荷:'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`  ]),
  ...mapActions({add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
  })
}

mapState

export const mapState = (stateArr) => {// {age:fn}
    let obj = {};
    stateArr.forEach(stateName => {obj[stateName] = function () {return this.$store.state[stateName]
        }
    });
    return obj;
}

mapGetters

export function mapGetters(gettersArr) {let obj = {};
  gettersArr.forEach(getterName => {obj[getterName] = function () {return this.$store.getters[getterName];
      }
  });
  return obj
}

mapMutations

export function mapMutations(obj) {let res = {};
  Object.entries(obj).forEach(([key, value]) => {res[key] = function (...args) {this.$store.commit(value, ...args)
      }
  })
  return res;
}

mapActions

export function mapActions(obj) {let res = {};
  Object.entries(obj).forEach(([key, value]) => {res[key] = function (...args) {this.$store.dispatch(value, ...args)
      }
  })
  return res;
}

插件

Vuex 的 store 承受 plugins 选项,这个选项暴露出每次 mutation 的钩子。Vuex 插件就是一个函数,它接管 store 作为惟一参数:

实际上 具体实现是公布订阅着模式,通过 store.subscribe 将须要执行的函数保留到 store subs 中,
当 state 值产生扭转时,this.subs(fn=>fn()) 执行。

const vuePersists = store => {let local = localStorage.getItem('VuexStore');
  if(local){store.replaceState(JSON.parse(local)); // 本地有则赋值
  }
  store.subscribe((mutation,state)=>{localStorage.setItem('VuexStore',JSON.stringify(state)); // state 发生变化执行
  });
}
const store = new Vuex.Store({
  // ...
  plugins: [vuePersists]
})
class Store{constructor () {this.subs = []
        const setMoutations = (data, path = []) => {
      const mutations = data.mutations
      const namespace = data.namespaced
      Object.keys(mutations).map(mutationName => {
        let namespace = mutationName
        if (namespace) {namespace = path.join('/').concat('/'+mutationName)
        }
        this.mutations[namespace] = this.mutations[namespace] || []
        this.mutations[namespace].push((payload)=>{ // 之前是间接 push

        mutations[item](options.state, payload)

        this.subs.forEach(fn => fn({ // state 产生扭转 则公布告诉给插件
            type: namespace,
            payload: payload
          }, options.state));

        })
      })
      const otherModules = data.modules || {}
      if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setMoutations(otherModules[item], path.concat(item)) // path.concat 不会批改 path 原来的值
        })
      }
    }
    setMoutations(options)

  }
  subscribe(fn) {this.subs.push(fn);
    }
}

State 解决

state 还没解决呢,别忘记咱们 用户传入的 state 只是分 module 传的,最终都要挂载到 state 中,见初始值和下图图片。实际上是数据格式转化,置信跟后端对接多的同学,考验解决数据格式的能力了。是的递归跑不了了。

  • 初始值
{
 state: {
    time: 1,
    userInfo: {
      avatar: '',
      account_name: '',
      name: ''
    },
  },
  modules: {
      report: {
      state: {title: '',},
    },
    part: {
        state: {title: '',},
            modules: {
        partChild: {
          state: {titleChild: '',},
        }
      }
    },
  }
}
  • 转化称上面这种格局

能够看到外围办法还是 path, path.slice 来获取每次递归的父结点。

 const setState = (data, path = []) => {if (path.length > 0) {let parentModule = path.slice(0, -1).reduce((next, prev)=>{return next[prev]
      }, options.state)
      Vue.set(parentModule, path[path.length - 1], data.state); // 为了 State 每个属性增加 get set 办法
    // parentModule[path[path.length - 1]] = data.state // 这样批改 Vue 是不会监听的
    }  
    const otherModules = data.modules || {}
    if (Object.keys(otherModules).length > 0){Object.keys(otherModules).map(item => {setState(otherModules[item], path.concat(item))
      })
    }
  }
  setState(options)

收集模块

原谅我,最重要的一块放到最初才说。下面所有办法都是基于用户传的 options (new Vuex.Store(options)) 来实现的。然而用户输出的咱们怎能轻易具体,咱们还是要对模块进行进一步格局解决,转化成咱们须要的数据。转化偏见下图。

咱们能够剖析出收集模块,理论也是递归,转化成固定格局数据 _children、state、rawModule。
外围代码是 register 办法,理论也是数据格式的转化。

总结

通篇看下来,还是须要本人手敲一下,在实际的过程中,能力发现问题(this 指向、父子结点判断、异步办法保留提醒的奇妙)。当你明确具体实现,那每次应用就轻而易举了,每一步应用都晓得干了什么的感觉真好。

正文完
 0