Hooke

add module

...@@ -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>
......
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,41 +3,76 @@ import Vuex from 'vuex' ...@@ -3,41 +3,76 @@ import Vuex from 'vuex'
3 3
4 Vue.use(Vuex) 4 Vue.use(Vuex)
5 5
6 -const store = new Vuex.Store({ 6 +const moduleA = {
7 state: { 7 state: {
8 - count: 0, 8 + module: 'A'
9 - size: 10, 9 + },
10 - isLogin: false, 10 + getters: {},
11 - todos: [{ 11 + mutations: {},
12 - id: 1, 12 + actions: {}
13 - text: '...', 13 +}
14 - done: true 14 +
15 - }, { 15 +const moduleB = {
16 - id: 2, 16 + state: {
17 - text: '...', 17 + module: 'B'
18 - done: false
19 - }],
20 - obj: {
21 - size: 1
22 - }
23 }, 18 },
24 - getters: { 19 + getters: {},
25 - doneTodos: state => { 20 + mutations: {},
26 - return state.todos.filter(todo => todo.done) 21 + actions: {}
22 +}
23 +
24 +const store = new Vuex.Store({
25 + state: {
26 + count: 0,
27 + size: 10,
28 + isLogin: false,
29 + todos: [{
30 + id: 1,
31 + text: '...',
32 + done: true
33 + }, {
34 + id: 2,
35 + text: '...',
36 + done: false
37 + }],
38 + obj: {
39 + size: 1
40 + }
41 + },
42 + getters: {
43 + doneTodos: state => {
44 + return state.todos.filter(todo => todo.done)
45 + },
46 + doneTodosCount: (state, getters) => {
47 + return getters.doneTodos.length
48 + }
49 + },
50 + mutations: {
51 + // increment (state, payload) {
52 + // state.count += payload.amount
53 + // },
54 + increment: state => state.count++,
55 + decrement: state => state.count--,
56 + login: state => state.isLogin = true,
57 + addObj: state => state.obj = {...state.obj,
58 + newProp: 123
59 + }
27 }, 60 },
28 - doneTodosCount: (state, getters) => { 61 + actions: {
29 - return getters.doneTodos.length 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
30 } 75 }
31 - },
32 - mutations: {
33 - // increment (state, payload) {
34 - // state.count += payload.amount
35 - // },
36 - increment: state => state.count++,
37 - decrement: state => state.count--,
38 - login: state => state.isLogin = true,
39 - addObj: state => state.obj = { ...state.obj, newProp: 123 }
40 - }
41 }) 76 })
42 77
43 export default store 78 export default store
......