diff --git a/src/store/index.js b/src/store/index.js index bbd2d54..80e7265 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,32 +1,35 @@ import {createStore} from "vuex"; +import {moduleA} from "@/store/modules/ModuleA"; +import {moduleB} from "@/store/modules/ModuleB"; export default createStore({ + modules: { + module_a: moduleA, + module_b: moduleB, + }, state: { - count: 2, + count: 3, }, getters: { // getter使用箭头函数来定义,默认必传参数时state,如果需要其余参数,可以使用嵌套头函数 - doubleCount: (state) => { - return state.count * 2 + getter: (state) => (n) => { + return state.count * n; }, - moreCount: (state) => (n) => { - return state.count * n - } }, mutations: { // payload用于携带参数 - increment(state, payload) { + mutation(state, payload) { state.count += payload.num } }, actions: { - inc_actions(content, payload) { + action(content, payload) { // 模拟异步操作 setTimeout(() => { // rep 模拟axios请求返回的数据 const rep = 3; if (content.state.count > 50) { - content.commit('increment', {num: rep+payload.num}); + content.commit('mutation', {num: rep + payload.num}); } }, 1000) } diff --git a/src/store/modules/ModuleA.js b/src/store/modules/ModuleA.js new file mode 100644 index 0000000..69c9967 --- /dev/null +++ b/src/store/modules/ModuleA.js @@ -0,0 +1,20 @@ +export const moduleA = { + state: { + state_a: 1, + }, + getters: { + getter_a: (state) => { + return state.state_a + 10; + } + }, + mutations: { + mutation_a(state, payload) { + state.state_a + payload.num; + } + }, + actions: { + action_a(content, payload){ + content.commit('mutation_a', {num: payload.num}) + } + } +} diff --git a/src/store/modules/ModuleB.js b/src/store/modules/ModuleB.js new file mode 100644 index 0000000..bbd8b38 --- /dev/null +++ b/src/store/modules/ModuleB.js @@ -0,0 +1,21 @@ +export const moduleB = { + namespaced: true, + state: { + state_b: 2, + }, + getters: { + getter_b: (state) => { + return state.state_b + 10; + } + }, + mutations: { + mutation_b: (state, payload) => { + state.state_b + payload.num; + } + }, + actions: { + action_b: (content, payload) => { + content.commit('mutation_b', {num: payload.num}) + } + } +} diff --git a/src/views/OptionsAPIView.vue b/src/views/OptionsAPIView.vue index 6608e49..e26436a 100644 --- a/src/views/OptionsAPIView.vue +++ b/src/views/OptionsAPIView.vue @@ -1,54 +1,59 @@