diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/App.vue b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/App.vue new file mode 100644 index 0000000..2ab91da --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/App.vue @@ -0,0 +1,13 @@ + + diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/main.js b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/main.js new file mode 100644 index 0000000..225b6e1 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/main.js @@ -0,0 +1,6 @@ +import {createApp} from "vue"; +import App from "./App.vue" +import router from './router' +import store from './store' + +createApp(App).use(router).use(store).mount('#app') diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/router/index.js b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/router/index.js new file mode 100644 index 0000000..619eba5 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/router/index.js @@ -0,0 +1,32 @@ +// 引入必要的router组件 +import {createRouter, createWebHistory} from 'vue-router' +// 引入视图 +const OptionsAPI = () => import('../views/OptionsAPIView.vue') +const CompositionAPI = () => import('../views/CompositionAPIView.vue') + +// 引入组件 + + +// 创建路由 +const routes = [ + { + path: '/options', + name: 'options', + component: OptionsAPI + }, + { + path: '/composition', + name: 'composition', + component: CompositionAPI + }, +] + +// 创建路由器,使用createRouter方法,传入history(路由类型)和 创建好的路由routes +// 路由类型分为hash型和html5型,分别为createWebHashHistory和createWebHistory +const router = createRouter({ + history: createWebHistory(), + routes +}) + +// 导出路由 +export default router diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/index.js b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/index.js new file mode 100644 index 0000000..80e7265 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/index.js @@ -0,0 +1,37 @@ +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: 3, + }, + getters: { + // getter使用箭头函数来定义,默认必传参数时state,如果需要其余参数,可以使用嵌套头函数 + getter: (state) => (n) => { + return state.count * n; + }, + }, + mutations: { + // payload用于携带参数 + mutation(state, payload) { + state.count += payload.num + } + }, + actions: { + action(content, payload) { + // 模拟异步操作 + setTimeout(() => { + // rep 模拟axios请求返回的数据 + const rep = 3; + if (content.state.count > 50) { + content.commit('mutation', {num: rep + payload.num}); + } + }, 1000) + } + } +}) diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleA.js b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleA.js new file mode 100644 index 0000000..00d45c5 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleA.js @@ -0,0 +1,22 @@ +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) { + if (content.state.state_a > 50) { + content.commit('mutation_a', {num: payload.num}) + } + } + } +} diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleB.js b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleB.js new file mode 100644 index 0000000..78412a5 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/store/modules/ModuleB.js @@ -0,0 +1,23 @@ +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) => { + if (content.state.state_b > 50) { + content.commit('mutation_b', {num: payload.num}) + } + }, + } +} diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/CompositionAPIView.vue b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/CompositionAPIView.vue new file mode 100644 index 0000000..093c864 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/CompositionAPIView.vue @@ -0,0 +1,79 @@ + + + + + diff --git a/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/OptionsAPIView.vue b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/OptionsAPIView.vue new file mode 100644 index 0000000..afef248 --- /dev/null +++ b/demo/vuex-mapState-mapGetters-mapMutations-mapActions/views/OptionsAPIView.vue @@ -0,0 +1,150 @@ + + + + +