Showing
3 changed files
with
67 additions
and
4 deletions
| ... | @@ -9,6 +9,7 @@ | ... | @@ -9,6 +9,7 @@ |
| 9 | <button @click="add">add</button> | 9 | <button @click="add">add</button> |
| 10 | <button @click="decrement">-</button> | 10 | <button @click="decrement">-</button> |
| 11 | <button @click="login">login</button> | 11 | <button @click="login">login</button> |
| 12 | + <button @click="incrementAsync">incrementAsync</button> | ||
| 12 | </p> | 13 | </p> |
| 13 | <child :level="1">Hello world!</child> | 14 | <child :level="1">Hello world!</child> |
| 14 | <child :level="3">Hello world!</child> | 15 | <child :level="3">Hello world!</child> |
| ... | @@ -16,6 +17,7 @@ | ... | @@ -16,6 +17,7 @@ |
| 16 | <getters></getters> | 17 | <getters></getters> |
| 17 | <span>obj: {{obj}}</span> | 18 | <span>obj: {{obj}}</span> |
| 18 | <button @click="addObj">addObj</button> | 19 | <button @click="addObj">addObj</button> |
| 20 | + <module></module> | ||
| 19 | </div> | 21 | </div> |
| 20 | </template> | 22 | </template> |
| 21 | 23 | ||
| ... | @@ -24,6 +26,7 @@ import counter from './components/counter.vue' | ... | @@ -24,6 +26,7 @@ import counter from './components/counter.vue' |
| 24 | import child from './components/child.vue' | 26 | import child from './components/child.vue' |
| 25 | import myParagraph from './components/myParagraph.vue' | 27 | import myParagraph from './components/myParagraph.vue' |
| 26 | import getters from './components/getters.vue' | 28 | import getters from './components/getters.vue' |
| 29 | +import module from './components/module.vue' | ||
| 27 | import { mapMutations } from 'vuex' | 30 | import { mapMutations } from 'vuex' |
| 28 | 31 | ||
| 29 | export default { | 32 | export default { |
| ... | @@ -65,17 +68,20 @@ export default { | ... | @@ -65,17 +68,20 @@ export default { |
| 65 | }, | 68 | }, |
| 66 | ...mapMutations({ | 69 | ...mapMutations({ |
| 67 | add: 'increment' // 映射 this.add() 到 this.$store.commit('increment') | 70 | add: 'increment' // 映射 this.add() 到 this.$store.commit('increment') |
| 68 | - }) | 71 | + }), |
| 69 | // ...mapMutations([ | 72 | // ...mapMutations([ |
| 70 | // 'increment' // 映射 this.increment() 到 this.$store.commit('increment') | 73 | // 'increment' // 映射 this.increment() 到 this.$store.commit('increment') |
| 71 | // ]), | 74 | // ]), |
| 72 | - | 75 | + incrementAsync() { |
| 76 | + this.$store.dispatch('incrementAsync') | ||
| 77 | + } | ||
| 73 | }, | 78 | }, |
| 74 | components: { | 79 | components: { |
| 75 | counter, | 80 | counter, |
| 76 | child, | 81 | child, |
| 77 | myParagraph, | 82 | myParagraph, |
| 78 | - getters | 83 | + getters, |
| 84 | + module | ||
| 79 | } | 85 | } |
| 80 | } | 86 | } |
| 81 | </script> | 87 | </script> | ... | ... |
src/components/module.vue
0 → 100644
| 1 | +<template lang="html"> | ||
| 2 | +<div> | ||
| 3 | + moduleA: {{moduleA}} | ||
| 4 | + moduleB: {{moduleB}} | ||
| 5 | +</div> | ||
| 6 | +</template> | ||
| 7 | + | ||
| 8 | +<script> | ||
| 9 | +export default { | ||
| 10 | + computed: { | ||
| 11 | + moduleA(){ | ||
| 12 | + return this.$store.state.a.module | ||
| 13 | + }, | ||
| 14 | + moduleB(){ | ||
| 15 | + return this.$store.state.b.module | ||
| 16 | + } | ||
| 17 | + } | ||
| 18 | +} | ||
| 19 | +</script> | ||
| 20 | + | ||
| 21 | +<style lang="css"> | ||
| 22 | +</style> |
| ... | @@ -3,6 +3,24 @@ import Vuex from 'vuex' | ... | @@ -3,6 +3,24 @@ import Vuex from 'vuex' |
| 3 | 3 | ||
| 4 | Vue.use(Vuex) | 4 | Vue.use(Vuex) |
| 5 | 5 | ||
| 6 | +const moduleA = { | ||
| 7 | + state: { | ||
| 8 | + module: 'A' | ||
| 9 | + }, | ||
| 10 | + getters: {}, | ||
| 11 | + mutations: {}, | ||
| 12 | + actions: {} | ||
| 13 | +} | ||
| 14 | + | ||
| 15 | +const moduleB = { | ||
| 16 | + state: { | ||
| 17 | + module: 'B' | ||
| 18 | + }, | ||
| 19 | + getters: {}, | ||
| 20 | + mutations: {}, | ||
| 21 | + actions: {} | ||
| 22 | +} | ||
| 23 | + | ||
| 6 | const store = new Vuex.Store({ | 24 | const store = new Vuex.Store({ |
| 7 | state: { | 25 | state: { |
| 8 | count: 0, | 26 | count: 0, |
| ... | @@ -36,7 +54,24 @@ const store = new Vuex.Store({ | ... | @@ -36,7 +54,24 @@ const store = new Vuex.Store({ |
| 36 | increment: state => state.count++, | 54 | increment: state => state.count++, |
| 37 | decrement: state => state.count--, | 55 | decrement: state => state.count--, |
| 38 | login: state => state.isLogin = true, | 56 | login: state => state.isLogin = true, |
| 39 | - addObj: state => state.obj = { ...state.obj, newProp: 123 } | 57 | + addObj: state => state.obj = {...state.obj, |
| 58 | + newProp: 123 | ||
| 59 | + } | ||
| 60 | + }, | ||
| 61 | + actions: { | ||
| 62 | + increment({ commit }) { | ||
| 63 | + // 对象解构 | ||
| 64 | + commit('increment') | ||
| 65 | + }, | ||
| 66 | + incrementAsync({ commit }) { | ||
| 67 | + setTimeout(() => { | ||
| 68 | + commit('increment') | ||
| 69 | + }, 1000) | ||
| 70 | + } | ||
| 71 | + }, | ||
| 72 | + modules: { | ||
| 73 | + a: moduleA, | ||
| 74 | + b: moduleB | ||
| 40 | } | 75 | } |
| 41 | }) | 76 | }) |
| 42 | 77 | ... | ... |
-
Please register or login to post a comment