mutations相当于组件中的methods,但是它不能使用异步方法(定时器,axios)
//在store(仓库)下的index.js这份文件,就是用来做状态管理
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
//mutations相当于组件中的methods,但是它不能使用异步方法(定时器,axios)
mutations:{
//让num累加
//payload是一个形参,如果组件在commit时,有传这个参数过来,就存在,如果没有传过来,就是undefined
increase(state,payload){
state.num += payload ? payload : 1;
}
}
})
Btn.vue
<template>
<div>
<button @click="$store.commit('increase',2)">
点击加1
</button>
</div>
</template>
<script>
export default {
}
</script>
<style>
</style>