main.js
const store = new Vuex.Store({ // 创建vuex实例
state: { // 仓库
count: 0
},
mutations: { // 修改
countIncrease (state) { // 把state传进来
state.count++
}
}
})
app.vue
<h1>count:{{count}}</h1>
<button @click="countIncrease">点我</button>
methods: {
countIncrease () {
this.$store.commit('countIncrease') // commit:同步操作,写法:this.$store.commit('mutations方法名',值)
}
},
computed: {
count () {
return this.$store.state.count // 获取vuex的数据
}
}