store/index.vue
import { createStore } from 'vuex'
export default createStore({
state: {
num:123
},
mutations: {
ADDNUM (state) {
state.num++
},
GETNUM (state,num) {
state.num = state.num * num
}
},
actions: {
ChengNum: ({commit},num) => {
setTimeout(() => {
commit("GETNUM",num)
},1000)
}
},
modules: {
}
})
获取执行
<template>
<div>
{{state.num}}
<button @click="add">加法</button>
<button @click="ride">乘法</button>
</div>
</template>
<script setup>
import {useStore} from 'vuex'
const {state,commit,dispatch} = useStore()
const add = () => {
commit('ADDNUM')
}
const ride = () => {
dispatch('ChengNum',3)
}
</script>
<style>
</style>