1、store文件夹的index.js文件下操作vuex分模块管理导入:
import { createStore } from 'vuex'
import appStore from './appStore'
export default createStore({
modules: {
appStore
}
})
2、store/appStore.js文件创建存取需求的操作方法
const appStore = {
state: {
totle: 10
},
mutations: {
totleFun(state, value) {
state.totle = value
}
},
actions: {
totleFunAdd(context, value) {
context.commit('totleFun', value + 1)
},
totleFunDel(context, value) {
context.commit('totleFun', value - 1)
}
}
}
export default appStore;
3、views/app.js文件下渲染、存取操作
<template>
<div class="app">{{store.state.appStore.totle}}</h1>
<button @click="add">+值</button> <button @click="del">-值</button>
</div>
</template>
<script>
import { useStore } from 'vuex';
export default {
setup() {
const store = useStore();
const add = () => {
store.dispatch('aFunAdd', store.state.appStore.totle)
}
const del = () => {
store.dispatch('aFunDel', store.state.appStore.totle)
}
return {
add,
del,
store
}
}
}
</script>