原始代码
错误原因:mutations中的increment用的小写,Vue建议用大写
解决方法:increment改成INCREMENT就可以了
const store = new Vuex.Store({
state: {
counter: 1000
},
mutations: {
//方法
increment(state) {
state.counter++
},
}
)}
<div id="app">
<h2>-----------app组件内容</h2>
<h2>{
{$store.state.counter}}</h2>
<button @click="addition">+</button>
<h2>-------vuex内容-----------</h2>
</div>
</template>
<script>
import HelloVuex from "./components/HelloWorld.vue";
export default {
name: "App",
components: {},
methods: {
addition() {
//调用type,触发handler(state)
this.$store.commit("increment");
},
改正后