Skip to content

Commit 03f52be

Browse files
vip30ktsn
authored andcommitted
Add vuex example (#250)
1 parent 6bbafec commit 03f52be

File tree

5 files changed

+80
-913
lines changed

5 files changed

+80
-913
lines changed

example/App.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
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'
1622
import Component from '../lib/index'
1723
import Hello from './Hello.vue'
1824
import 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
})
3450
export 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

example/example.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Vue from 'vue'
22
import App from './App.vue'
3+
import store from './store'
34

45
// mount
56
new Vue({
67
el: '#app',
8+
store,
79
render: h => h(App, {
810
props: { propMessage: 'World' }
911
})

example/store.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
})

0 commit comments

Comments
 (0)