---------------------store.js-----------------------------------
const store = new Vuex.Store({
state: {
count: 100000000
},
getters: {
getChangedNum () {
// eslint-disable-next-line no-undef
return state.count
}
},
mutations: {
newNum (state, sum) {
state.count += sum
}
},
actions: {
getNewNum (context, num) {
context.commit('newNum', num)
}
}
})
----------------组件-----------------------
<button @click="getNewNum">add</button>
methods: {
add () {
this.$store.commit('newNum', 1) // 调用mutations
this.$store.dispatch('getNewNum', 1) // 调用actions ,
}
...mapActions({
add: 'getNewNum' //如果使用mapActions, 这里怎么要传递这个‘1’
})
}
pardon110
相关分类