状态管理Vuex 之 Mutation

  • 原创
  • 作者:程序员三丰
  • 发布时间:2021-08-03 11:42
  • 浏览量:373
mutation是Vuex中store数据改变的唯一方法,形式上类似于事件。通俗的理解,mutations 里面装着改变数据的方法集合,处理数据逻辑的方法全部放在 mutations 里,使数据和视图分离。

状态管理Vuex 之 Mutation

Mutation 概念介绍

  • Vuex 中 store 数据改变的唯一方法就是 mutation 。
  • mutations 里面装着改变数据的方法集合,处理数据逻辑的方法全部都会放在 mutations 里,使数据和视图分离。

Mutation 代码构建

  • muation 非常类似于事件:每个 mutation 都有一个字符串的事件类型(type)和一个回调函数(handler)。这个回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数。

    /* 文件路径:store/index.js */
    import Vue from 'vue'
    import Vuex from 'vuex'
    
    Vue.use(Vuex)
    
    const store = new Vuex.Store({
        state: {
            count: 1
        },
        mutations: {
            add(state) {
                // 变更状态
                state.count += 2
            }
        }
    })
    
    export default store
    

    注意:不能直接调用一个 mutation hanlder。 这个选项更像是事件注册:

    • 当触发一个类型为 add 的 mutation 时,调用此函数。
    • 要唤醒一个 mutation handler 你需要以相应的 type 调用 store.commit 方法。

Mutation 调用 && 提交

  • store.commit 调用 mutation (需要在根节点注入 store)

    <!-- 页面路径:pages/index/index.vue -->
    <template>
        <view>数量:{{count}}</view>
        <button @click="addCount">增加</button>
    </template>
    <script>
        import store from '@/store/index.js'
        export default {
            computed: {
                count() {
                    return this.$store.state.count
                },
            }
            methods: {
                addCount() {
                    store.commit('add')
                }
            }    
        }
    </script>
    
  • 传入参数

    向 store.commit 传入额外的参数,即 mutation 的荷载(payload):

    • 可以传递一个参数

      /* 文件路径:store/index.js */
      import Vue from 'vue'
      import Vuex from 'vuex'
      
      Vue.use(Vuex)
      
      const store = new Vuex.Store({
          state: {
              count: 1
          },
          mutations: {
              add(state, n) {
                  // 变更状态
                  state.count += n
              }
          }
      })
      
      export default store
      
      <!-- 页面路径:pages/index/index.vue -->
      <template>
          <view>数量:{{count}}</view>
          <button @click="addCount">增加</button>
      </template>
      <script>
          import store from '@/store/index.js'
          export default {
              computed: {
                  count() {
                      return this.$store.state.count
                  },
              }
              methods: {
                  addCount() {
                      store.commit('add', 5) // 每次累加5
                  }
              }    
          }
      </script>
      
    • 可以传递一个对象

      /* 文件路径:store/index.js */
      import Vue from 'vue'
      import Vuex from 'vuex'
      
      Vue.use(Vuex)
      
      const store = new Vuex.Store({
          state: {
              count: 1
          },
          mutations: {
              add(state, payload) {
                  // 变更状态
                  state.count += payload.amount
              }
          }
      })
      
      export default store
      
      <!-- 页面路径:pages/index/index.vue -->
      <template>
          <view>数量:{{count}}</view>
          <button @click="addCount">增加</button>
      </template>
      <script>
          import store from '@/store/index.js'
          export default {
              computed: {
                  count() {
                      return this.$store.state.count
                  },
              }
              methods: {
                  addCount() {
                      store.commit('add', {aount: 10}) // 每次累加10, 把荷载和type分开提交
                  }
              }    
          }
      </script>
      
  • 提交方式

    • 对象风格的提交方式,handler 保持不变

      <!-- 页面路径:pages/index/index.vue -->
      <template>
          <view>数量:{{count}}</view>
          <button @click="addCount">增加</button>
      </template>
      <script>
          import store from '@/store/index.js'
          export default {
              computed: {
                  count() {
                      return this.$store.state.count
                  },
              }
              methods: {
                  addCount() {
                      // 整个对象都作为荷载传给 mutation 函数
                      store.commit({
                          type:'add',
                          aount: 10
                      }) 
                  }
              }    
          }
      </script>
      
    • 通过 mapMutations 辅助函数提交

      使用 mapMutations 辅助函数将组件中的 methods 映射为 store.commit 调用(需要在根节点注入 store)。

      <!-- 页面路径:pages/index/index.vue -->
      <template>
          <view>数量:{{count}}</view>
          <button @click="add">增加</button>
      </template>
      <script>
          import mapMutations from 'vuex' //引入 mapMutations
          export default {
              computed: {
                  count() {
                      return this.$store.state.count
                  },
              }
              methods: {
                  ...mapMutations(['add']) //对象展开运算符直接拿到add
              }    
          }
      </script>
      
      /* 文件路径:store/index.js */
      import Vue from 'vue'
      import Vuex from 'vuex'
      
      Vue.use(Vuex)
      
      const store = new Vuex.Store({
          state: {
              count: 1
          },
          mutations: {
              add(state) {
                  // 变更状态
                  state.count += 2
              }
          }
      })
      
      export default store
      

规则与约定

  • 最好提前在 store 中初始化好所有所需属性。
  • mutation 必须是同步函数。
声明:本文为原创文章,51blog.xyz和作者拥有版权,如需转载,请注明来源于51blog.xyz并保留原文链接:https://www.51blog.xyz/article/20

文章归档

强烈推荐的PHP全栈开发后台管理系统
buildadmin logo
Thinkphp8 Vue3 Element PLus TypeScript Vite Pinia

🔥BuildAdmin是一个永久免费开源,无需授权即可商业使用,且使用了流行技术栈快速创建商业级后台管理系统。

推荐文章

热门标签

PHP ThinkPHP ThinkPHP5.1 Go Mysql Mysql5.7 Redis Linux CentOS7 Git HTML CSS CSS3 Javascript JQuery Vue LayUI VMware Uniapp 微信小程序 docker wiki Confluence7 学习笔记 uView ES6 Ant Design Pro of Vue React ThinkPHP6.0 chrome 扩展 翻译工具 Nuxt SSR 服务端渲染 scrollreveal.js ThinkPHP8.0 Mac webman 跨域CORS vscode GitHub ECharts Canvas vue3 three.js 微信支付 PHP全栈开发 Python AI 人工智能 AI辅助 工作经验 实战笔记