File tree Expand file tree Collapse file tree 5 files changed +80
-913
lines changed Expand file tree Collapse file tree 5 files changed +80
-913
lines changed Original file line number Diff line number Diff line change 8
8
<Hello ref =" helloComponent" />
9
9
<World />
10
10
<button @click =" greet" >Greet</button >
11
+
12
+ Clicked: {{ $store.state.count }} times, count is {{ evenOrOdd }}.
13
+ <button @click =" increment" >+</button >
14
+ <button @click =" decrement" >-</button >
15
+ <button @click =" incrementIfOdd" >Increment if odd</button >
16
+ <button @click =" incrementAsync" >Increment async</button >
11
17
</div >
12
18
</template >
13
19
@@ -16,6 +22,7 @@ import Vue from 'vue'
16
22
import Component from ' ../lib/index'
17
23
import Hello from ' ./Hello.vue'
18
24
import World from ' ./World'
25
+ import { mapGetters , mapActions } from ' vuex'
19
26
20
27
// We declare the props separately
21
28
// to make props types inferable.
@@ -29,7 +36,16 @@ const AppProps = Vue.extend({
29
36
components: {
30
37
Hello ,
31
38
World
32
- }
39
+ },
40
+ // mapGetters & mapActions example
41
+ computed: mapGetters ([
42
+ ' evenOrOdd'
43
+ ]),
44
+ methods: mapActions ([
45
+ ' increment' ,
46
+ ' decrement' ,
47
+ ' incrementAsync'
48
+ ])
33
49
})
34
50
export default class App extends AppProps {
35
51
// inital data
@@ -54,6 +70,11 @@ export default class App extends AppProps {
54
70
this .$refs .helloComponent .sayHello ()
55
71
}
56
72
73
+ // direct dispatch example
74
+ incrementIfOdd() {
75
+ this .$store .dispatch (' incrementIfOdd' )
76
+ }
77
+
57
78
// dynamic component
58
79
$refs! : {
59
80
helloComponent: Hello
Original file line number Diff line number Diff line change 1
1
import Vue from 'vue'
2
2
import App from './App.vue'
3
+ import store from './store'
3
4
4
5
// mount
5
6
new Vue ( {
6
7
el : '#app' ,
8
+ store,
7
9
render : h => h ( App , {
8
10
props : { propMessage : 'World' }
9
11
} )
Original file line number Diff line number Diff line change
1
+ import Vue from 'vue'
2
+ import Vuex , { ActionContext } from "vuex"
3
+ interface CounterState {
4
+ count : number
5
+ }
6
+
7
+ Vue . use ( Vuex )
8
+
9
+ const state = {
10
+ count : 0
11
+ }
12
+
13
+ const mutations = {
14
+ increment ( state : CounterState ) {
15
+ state . count ++
16
+ } ,
17
+ decrement ( state : CounterState ) {
18
+ state . count --
19
+ }
20
+ }
21
+
22
+ const actions = {
23
+ increment : ( context : ActionContext < CounterState , any > ) => context . commit ( 'increment' ) ,
24
+ decrement : ( context : ActionContext < CounterState , any > ) => context . commit ( 'decrement' ) ,
25
+ incrementIfOdd ( context : ActionContext < CounterState , any > ) {
26
+ if ( ( context . state . count + 1 ) % 2 === 0 ) {
27
+ context . commit ( 'increment' )
28
+ }
29
+ } ,
30
+ incrementAsync ( context : ActionContext < CounterState , any > ) {
31
+ return new Promise ( ( resolve ) => {
32
+ setTimeout ( ( ) => {
33
+ context . commit ( 'increment' )
34
+ resolve ( )
35
+ } , 1000 )
36
+ } )
37
+ }
38
+ }
39
+
40
+ const getters = {
41
+ evenOrOdd : ( state : CounterState ) => state . count % 2 === 0 ? 'even' : 'odd'
42
+ }
43
+
44
+ export default new Vuex . Store ( {
45
+ state,
46
+ getters,
47
+ actions,
48
+ mutations
49
+ } )
You can’t perform that action at this time.
0 commit comments