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 88 <Hello ref =" helloComponent" />
99 <World />
1010 <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 >
1117 </div >
1218</template >
1319
@@ -16,6 +22,7 @@ import Vue from 'vue'
1622import Component from ' ../lib/index'
1723import Hello from ' ./Hello.vue'
1824import World from ' ./World'
25+ import { mapGetters , mapActions } from ' vuex'
1926
2027// We declare the props separately
2128// to make props types inferable.
@@ -29,7 +36,16 @@ const AppProps = Vue.extend({
2936 components: {
3037 Hello ,
3138 World
32- }
39+ },
40+ // mapGetters & mapActions example
41+ computed: mapGetters ([
42+ ' evenOrOdd'
43+ ]),
44+ methods: mapActions ([
45+ ' increment' ,
46+ ' decrement' ,
47+ ' incrementAsync'
48+ ])
3349})
3450export default class App extends AppProps {
3551 // inital data
@@ -54,6 +70,11 @@ export default class App extends AppProps {
5470 this .$refs .helloComponent .sayHello ()
5571 }
5672
73+ // direct dispatch example
74+ incrementIfOdd() {
75+ this .$store .dispatch (' incrementIfOdd' )
76+ }
77+
5778 // dynamic component
5879 $refs! : {
5980 helloComponent: Hello
Original file line number Diff line number Diff line change 11import Vue from 'vue'
22import App from './App.vue'
3+ import store from './store'
34
45// mount
56new Vue ( {
67 el : '#app' ,
8+ store,
79 render : h => h ( App , {
810 props : { propMessage : 'World' }
911 } )
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