Vuex之理解Mutations

    •  
      • 通俗的理解mutations,里面装着一些改变数据方法的集合,这是Veux设计很重要的一点,就是把处理数据逻辑方法全部放在mutations里面,使得数据和视图分离。

      • 2.怎么用mutations

        • mutation结构:每一个mutation都有一个字符串类型的事件类型(type)和回调函数(handler),也可以理解为{type:handler()},这和订阅发布有点类似。先注册事件,当触发响应类型的时候调用handker(),调用type的时候需要用到store.commit方法。

           
          1. const store = new Vuex.Store({

          2. state: {

          3. count: 1

          4. },

          5. mutations: {

          6. increment (state) { //注册事件,type:increment,handler第一个参数是state;

          7. // 变更状态

          8. state.count++}}})

          9.  
          10. store.commit('increment') //调用type,触发handler(state)

        • 载荷(payload):简单的理解就是往handler(stage)中传参handler(stage,pryload);一般是个对象。

           
          1. mutations: {

          2. increment (state, n) {

          3. state.count += n}}

          4. store.commit('increment', 10)

        • mutation-types:将常量放在单独的文件中,方便协作开发。

           
          1. // mutation-types.js

          2. export const SOME_MUTATION = 'SOME_MUTATION'

          3. // store.js

          4. import Vuex from 'vuex'

          5. import { SOME_MUTATION } from './mutation-types'

          6.  
          7. const store = new Vuex.Store({

          8. state: { ... },

          9. mutations: {

          10. // 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名

          11. [SOME_MUTATION] (state) {

          12. // mutate state

          13. }

          14. }

          15. })

        • commit:提交可以在组件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

           
          1. import { mapMutations } from 'vuex'

          2.  
          3. export default {

          4.  
          5. methods: {

          6. ...mapMutations([

          7. 'increment' // 映射 this.increment() 为

          8. this.$store.commit('increment')]),

          9. ...mapMutations({

          10. add: 'increment' // 映射 this.add() 为

          11. this.$store.commit('increment')

          12. })}}


        3.源码分析

        • registerMutation:初始化mutation

           
          1. function registerMutation (store, type, handler, path = []) {

          2. //4个参数,store是Store实例,type为mutation的type,handler,path为当前模块路径

          3. const entry = store._mutations[type] || (store._mutations[type] =

          4. []) //通过type拿到对应的mutation对象数组

          5. entry.push(function wrappedMutationHandler (payload) {

          6. //将mutation包装成函数push到数组中,同时添加载荷payload参数

          7. handler(getNestedState(store.state, path), payload)

          8. //通过getNestedState()得到当前的state,同时添加载荷payload参数

          9. })

          10. }

        • commit:调用mutation

           
          1. commit (type, payload, options) {

          2. // 3个参数,type是mutation类型,payload载荷,options配置

          3. if (isObject(type) && type.type) {

          4. // 当type为object类型,

          5. options = payload

          6. payload = type

          7. type = type.type

          8. }

          9. const mutation = { type, payload }

          10. const entry = this._mutations[type]

          11. // 通过type查找对应的mutation

          12. if (!entry) {

          13. //找不到报错

          14. console.error(`[vuex] unknown mutation type: ${type}`)

          15. return

          16. }

          17. this._withCommit(() => {

          18. entry.forEach(function commitIterator (handler) {

          19. // 遍历type对应的mutation对象数组,执行handle(payload)方法

          20. //也就是开始执行wrappedMutationHandler(handler)

          21. handler(payload)

          22. })

          23. })

          24. if (!options || !options.silent) {

          25. this._subscribers.forEach(sub => sub(mutation, this.state))

          26. //把mutation和根state作为参数传入

          27. }

          28. }

        • subscribers:订阅storemutation

           
          1. subscribe (fn) {

          2. const subs = this._subscribers

          3. if (subs.indexOf(fn) < 0) {

          4. subs.push(fn)

          5. }

          6. return () => {

          7. const i = subs.indexOf(fn)

          8. if (i > -1) {

          9. subs.splice(i, 1)

          10. }

          11. }

          12. }

猜你喜欢

转载自blog.csdn.net/qq_36838191/article/details/82383816
今日推荐