Hooke

add mutations

...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
2 <div id="app"> 2 <div id="app">
3 <counter></counter> 3 <counter></counter>
4 <div class=""> 4 <div class="">
5 - {{isLogin}} 5 + {{isLogin}}
6 </div> 6 </div>
7 <p> 7 <p>
8 - <button @click="increment">+</button> 8 + <!-- <button @click="increment">+</button> -->
9 + <button @click="add">add</button>
9 <button @click="decrement">-</button> 10 <button @click="decrement">-</button>
10 <button @click="login">login</button> 11 <button @click="login">login</button>
11 </p> 12 </p>
...@@ -13,6 +14,8 @@ ...@@ -13,6 +14,8 @@
13 <child :level="3">Hello world!</child> 14 <child :level="3">Hello world!</child>
14 <my-paragraph></my-paragraph> 15 <my-paragraph></my-paragraph>
15 <getters></getters> 16 <getters></getters>
17 + <span>obj: {{obj}}</span>
18 + <button @click="addObj">addObj</button>
16 </div> 19 </div>
17 </template> 20 </template>
18 21
...@@ -21,6 +24,7 @@ import counter from './components/counter.vue' ...@@ -21,6 +24,7 @@ import counter from './components/counter.vue'
21 import child from './components/child.vue' 24 import child from './components/child.vue'
22 import myParagraph from './components/myParagraph.vue' 25 import myParagraph from './components/myParagraph.vue'
23 import getters from './components/getters.vue' 26 import getters from './components/getters.vue'
27 +import { mapMutations } from 'vuex'
24 28
25 export default { 29 export default {
26 name: 'app', 30 name: 'app',
...@@ -31,22 +35,47 @@ export default { ...@@ -31,22 +35,47 @@ export default {
31 }, 35 },
32 computed: { 36 computed: {
33 isLogin() { 37 isLogin() {
34 - return this.$store.state.isLogin 38 + return this.$store.state.isLogin
39 + },
40 + obj () {
41 + return this.$store.state.obj
35 } 42 }
36 }, 43 },
37 methods: { 44 methods: {
38 - increment() { 45 + // increment() {
39 - this.$store.commit('increment') 46 + // this.$store.commit('increment', {
40 - }, 47 + // amount: 10
48 + // })
49 + // // this.$store.commit('increment', {
50 + // // amount: 10
51 + // // },{silent:true})
52 + // // this.$store.commit({
53 + // // type:'increment',
54 + // // amount: 10
55 + // // })
56 + // },
41 decrement() { 57 decrement() {
42 this.$store.commit('decrement') 58 this.$store.commit('decrement')
43 }, 59 },
44 login() { 60 login() {
45 this.$store.commit('login') 61 this.$store.commit('login')
46 - } 62 + },
63 + addObj() {
64 + this.$store.commit('addObj')
65 + },
66 + ...mapMutations({
67 + add: 'increment' // 映射 this.add() 到 this.$store.commit('increment')
68 + })
69 + // ...mapMutations([
70 + // 'increment' // 映射 this.increment() 到 this.$store.commit('increment')
71 + // ]),
72 +
47 }, 73 },
48 components: { 74 components: {
49 - counter,child,myParagraph,getters 75 + counter,
76 + child,
77 + myParagraph,
78 + getters
50 } 79 }
51 } 80 }
52 </script> 81 </script>
......
...@@ -16,7 +16,10 @@ const store = new Vuex.Store({ ...@@ -16,7 +16,10 @@ const store = new Vuex.Store({
16 id: 2, 16 id: 2,
17 text: '...', 17 text: '...',
18 done: false 18 done: false
19 - }] 19 + }],
20 + obj: {
21 + size: 1
22 + }
20 }, 23 },
21 getters: { 24 getters: {
22 doneTodos: state => { 25 doneTodos: state => {
...@@ -27,9 +30,13 @@ const store = new Vuex.Store({ ...@@ -27,9 +30,13 @@ const store = new Vuex.Store({
27 } 30 }
28 }, 31 },
29 mutations: { 32 mutations: {
33 + // increment (state, payload) {
34 + // state.count += payload.amount
35 + // },
30 increment: state => state.count++, 36 increment: state => state.count++,
31 decrement: state => state.count--, 37 decrement: state => state.count--,
32 - login: state => state.isLogin = true 38 + login: state => state.isLogin = true,
39 + addObj: state => state.obj = { ...state.obj, newProp: 123 }
33 } 40 }
34 }) 41 })
35 42
......