Vuex 改变 state

./store/state.js

export default {
  count: 3,
  name: '李雷',
}

./store/mutations.js

export default {
  INCREMENT(state) {
    state.count++
  },
}

./App.vue

<template>
  <div id="app">
    <!-- 改变方式1 -->
    <button @click="$store.commit('INCREMENT')">{{ name }}: {{ $store.state.count }}</button>
    <button @click="INCREMENT">{{ name }}: {{ $store.state.count }}</button>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'App',
  computed: {
    ...mapState(['name']),
  },
  methods: {
    ...mapMutations(['INCREMENT']), // 改变方式2
  },
}
</script>
原文地址:https://www.cnblogs.com/aisowe/p/15250011.html