Skip to content

Commit b2629cd

Browse files
committed
docs(example): simplify example
1 parent 3d1634b commit b2629cd

File tree

3 files changed

+27
-48
lines changed

3 files changed

+27
-48
lines changed

example/src/App.vue

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
<p>computed msg: {{ computedMsg }}</p>
88
<Hello ref="helloComponent" />
99
<World />
10-
<button @click="greet">Greet</button>
1110

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+
<p>
12+
<button @click="greet">Greet</button>
13+
</p>
14+
15+
<p>
16+
Clicked: {{ count }} times
17+
<button @click="increment">+</button>
18+
</p>
1719
</div>
1820
</template>
1921

@@ -22,7 +24,7 @@ import Vue from 'vue'
2224
import Component from '../../lib/index'
2325
import Hello from './components/Hello.vue'
2426
import World from './components/World'
25-
import { mapGetters, mapActions } from 'vuex'
27+
import { mapState, mapMutations } from 'vuex'
2628
2729
// We declare the props separately
2830
// to make props types inferable.
@@ -37,14 +39,13 @@ const AppProps = Vue.extend({
3739
Hello,
3840
World
3941
},
40-
// mapGetters & mapActions example
41-
computed: mapGetters([
42-
'evenOrOdd'
42+
43+
// Vuex's component binding helper can use here
44+
computed: mapState([
45+
'count'
4346
]),
44-
methods: mapActions([
45-
'increment',
46-
'decrement',
47-
'incrementAsync'
47+
methods: mapMutations([
48+
'increment'
4849
])
4950
})
5051
export default class App extends AppProps {
@@ -54,6 +55,16 @@ export default class App extends AppProps {
5455
// use prop values for initial data
5556
helloMsg: string = 'Hello, ' + this.propMessage
5657
58+
// annotate refs type
59+
$refs!: {
60+
helloComponent: Hello
61+
}
62+
63+
// additional declaration is needed
64+
// when you declare some properties in `Component` decorator
65+
count!: number
66+
increment!: () => void
67+
5768
// lifecycle hook
5869
mounted () {
5970
this.greet()
@@ -74,10 +85,5 @@ export default class App extends AppProps {
7485
incrementIfOdd() {
7586
this.$store.dispatch('incrementIfOdd')
7687
}
77-
78-
// dynamic component
79-
$refs!: {
80-
helloComponent: Hello
81-
}
8288
}
8389
</script>

example/src/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Vue from 'vue'
22
import App from './App.vue'
33
import store from './store'
44

5-
// mount
65
new Vue({
76
el: '#app',
87
store,

example/src/store.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue'
2-
import Vuex, { ActionContext } from "vuex"
2+
import Vuex from "vuex"
3+
34
interface CounterState{
45
count: number
56
}
@@ -13,37 +14,10 @@ const state = {
1314
const mutations = {
1415
increment (state: CounterState) {
1516
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-
})
3717
}
3818
}
3919

40-
const getters = {
41-
evenOrOdd: (state: CounterState) => state.count % 2 === 0 ? 'even' : 'odd'
42-
}
43-
4420
export default new Vuex.Store({
4521
state,
46-
getters,
47-
actions,
4822
mutations
4923
})

0 commit comments

Comments
 (0)