Vuex之mutation和action

mutation

同步操作

基本使用

创建文件

创建 client/store/mutations/

创建 client/store/state/mutations.js

声明 mutations

// mutaions.js
export default {
  updateCount (state, num) {
    state.count = num
  }
}

引用 mutations

// store.js
import defaultState from './state/state'
import mutations from './mutations/mutations' // 引用
import getters from './getters/getters'

export default () => {
  return new Vuex.Store({
    state: defaultState, 
    mutations, // 注入
    getters
  })
}

使用 mutaions

<template>
  <div id="app">
    <p>{{count}}</p>
</template>
<script>
export default {
  mounted () {
    let i = 1
    setInterval(() => {
      this.$store.commit('updateCount', i++)
    }, 1000)
  },
    computed: {
        ...mapState({
          counter: (state) => state.count
        })
      }
}
</script>

mutations传多个参数

commit 方法只能接受两个参数,第一个参数是 state,第二参数,以对象的形式传。

// app.vue
<script>
export default {
  mounted () {
    console.log(this.$store)
    let i = 1
    setInterval(() => {
      this.$store.commit('updateCount', {
        num: i++,
        num2: 2
      })
    }, 1000)
  }
}
</script>
export default {
  updateCount (state, { num, num2 }) { // es6 解构
    console.log(num2) // 打印出 2
    state.count = num
  }
}

store 使用规范

我们可以不通过 commit 来进行操作,但官方推荐都通过 commit,所以尽量杜绝不通过 commit 的操作。

通过设置生产环境严格模式,来进行代码规范。

import Vuex from 'vuex'

import defaultState from './state/state'
import mutations from './mutations/mutations'
import getters from './getters/getters'

const isDev = process.env.NODE_ENV === 'development' // 注意,正式环境不能使用 strict
export default () => {
  return new Vuex.Store({
    strict: isDev, // 添加严格模式
    state: defaultState, 
    mutations,
    getters
  })
}

actions

异步操作

基本使用

创建文件

创建 client/store/actions/

创建 client/store/state/actions.js

声明 actions

// actions.js
export default {
  updateCountAsync (store, data) {
    setTimeout(() => {
      store.commit('updateCount', {
        num: data.num
      })
    }, data.time)
  }
}

引用 actions

// store.js
import defaultState from './state/state'
import mutations from './mutations/mutations' // 引用
import getters from './getters/getters'
import actions from './actions/actions'

export default () => {
  return new Vuex.Store({
    state: defaultState, 
    mutations, 
    getters,
    actions // 注入
  })
}

使用 actions

<template>
  <div id="app">
    <p>{{count}}</p>
</template>
<script>
export default {
  mounted () {
   this.$store.dispatch('updateCountAsync', {
      num: 5,
      time: 2000
    })
  },
    computed: {
        ...mapState({
          counter: (state) => state.count
        })
      }
}
</script>

简写帮助方法

mapMutations 和 mapActions

<script>
import {
  mapState,
  mapGetters,
  mapActions,
  mapMutations
} from 'vuex'
export default {
  mounted () {
    let i = 1
    setInterval(() => {
      this.updateCount({  // 调用方式也变了
        num: i++,
        num2: 2
      })
    }, 1000)
    this.updateCountAsync({ // 调用方式也变了
      num: 5,
      time: 2000
    })
  },
  computed: {
    ...mapState({
      counter: (state) => state.count
    }),
    ...mapGetters(['fullName'])
  },
  methods: {
    ...mapActions(['updateCountAsync']), // actions 简写
    ...mapMutations(['updateCount']) // mutations 简写
  }
}
</script>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 8,043评论 0 7
  • vuex是什么鬼? 如果你用过redux就能很快的理解vuex是个什么鬼东西了。他是vuejs用来管理状态的插件。...
    麦子_FE阅读 11,817评论 3 37
  • Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件...
    萧玄辞阅读 8,332评论 0 6
  • 姓名:岳沁 学号:17101223458 转载自:http://blog.csdn.net/h5_queensty...
    丘之心阅读 6,471评论 0 1
  • vuex 场景重现:一个用户在注册页面注册了手机号码,跳转到登录页面也想拿到这个手机号码,你可以通过vue的组件化...
    sunny519111阅读 12,466评论 4 111

友情链接更多精彩内容