Skip to content

Commit b57e84b

Browse files
committed
Work on a more comprehensive vuex tutorial
1 parent 6738f59 commit b57e84b

File tree

3 files changed

+253
-0
lines changed

3 files changed

+253
-0
lines changed

docs/en/tutorial.md

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
# Tutorial
2+
3+
Let's build a very simple app which will demonstrate the various parts of vuex and how they work together. For this example we're building an app where you press a button and it increments a counter.
4+
5+
![End Result](tutorial/result.png)
6+
7+
We are using this simple example to explain the concept and the problem vuex aims to solve - how to manage a large app which uses several components. Consider if this example used three components:
8+
9+
### `components/App.vue`
10+
11+
The main app which contains two other child components:
12+
13+
* `Display` to display the current counter value.
14+
* `Increment` which is a button to increment the current value.
15+
16+
```html
17+
<template>
18+
<div>
19+
<Display></Display>
20+
<Increment></Increment>
21+
</div>
22+
</template>
23+
24+
<script>
25+
26+
import Display from "./Display.vue"
27+
import Increment from "./Increment.vue"
28+
29+
export default {
30+
components: {
31+
Display,
32+
Increment
33+
}
34+
}
35+
</script>
36+
```
37+
38+
### `components/Display.vue`
39+
40+
```
41+
<template>
42+
<div>
43+
<h3>Count is 0</h3>
44+
</div>
45+
</template>
46+
47+
<script>
48+
export default {
49+
}
50+
</script>
51+
```
52+
53+
### `components/Increment.vue`
54+
55+
```
56+
<template>
57+
<div>
58+
<button>Increment +1</button>
59+
</div>
60+
</template>
61+
62+
<script>
63+
export default {
64+
}
65+
</script>
66+
```
67+
68+
### Challenges
69+
70+
* `Increment` and `Display` aren't aware of each other and cannot pass messages to each other.
71+
* `App` will have to use events and broadcasts to coordinate the two components.
72+
* Since `App` is coordinating between the two components, they are not re-usable and tightly coupled. Re-structuring the app might break it
73+
74+
### Vuex "flow"
75+
76+
These are the steps that take place in order:
77+
78+
![Vuex Flow](tutorial/vuex_flow.png)
79+
80+
This might seem a little excessive for incrementing a counter. But do note that these concepts work well in larger applications and improve maintainability and make your app easier to debug and improve in the long run. So let's modify our app to use vuex.
81+
82+
### Step 1: Add a store
83+
84+
First, install vuex via npm:
85+
86+
```
87+
$ npm install --save vuex
88+
```
89+
90+
Create a new file in `vuex/store.js`
91+
92+
```js
93+
import Vue from 'vue'
94+
import Vuex from 'vuex'
95+
96+
// Make vue aware of vuex
97+
Vue.use(Vuex)
98+
99+
// We create an object to hold the initial state when
100+
// the app starts up
101+
const state = {
102+
// TODO: Set up our initial state
103+
}
104+
105+
// Create an object storing various mutations. We will write the mutation
106+
const mutations = {
107+
// TODO: set up our mutations
108+
}
109+
110+
// We combine the intial state and the mutations to create a vuex store.
111+
// This store can be linked to our app.
112+
export default new Vuex.Store({
113+
state,
114+
mutations
115+
})
116+
```
117+
118+
We need to make our app aware of this store. To do this we simply need to modify our root component.
119+
120+
Edit `components/App.vue` and add the store.
121+
122+
```js
123+
import Display from "./Display.vue"
124+
import Increment from "./IncrementButton.vue"
125+
import store from '../vuex/store' // import the store
126+
127+
export default {
128+
components: {
129+
Display,
130+
Increment
131+
},
132+
store: store // make this and all child components aware of the new store
133+
}
134+
```
135+
136+
### Step 2: Set up the action
137+
138+
Create a new file in `vuex/actions.js` with a single function `incrementCounter`
139+
140+
```
141+
// An action will recieve the store as the first argument.
142+
// Since we are only interested in the dispatch (and optionally the state)
143+
export const incrementCounter = function ({ dispatch, state }) {
144+
dispatch('INCREMENT', 1)
145+
}
146+
```
147+
148+
And let's call the action from our `components/Increment.vue` component.
149+
150+
```
151+
<template>
152+
<div>
153+
<button @click="increment">Increment +1</button>
154+
</div>
155+
</template>
156+
157+
<script>
158+
import { incrementCounter } from "../vuex/actions"
159+
export default {
160+
vuex: {
161+
actions: {
162+
increment: incrementCounter
163+
}
164+
}
165+
}
166+
</script>
167+
```
168+
169+
Notice some interesting things about what we just added.
170+
171+
1. We have a new object `vuex.actions` which includes the new action
172+
2. We didn't specify which store, object, state etc. Vuex wires everything up for us.
173+
3. We can call the action either by using `this.increment()` in any method.
174+
4. We can also call the action using the `@click` parameter making `increment` like any regular vue component method.
175+
5. The action is called `incrementCounter` but we can use any name which is appropriate.
176+
177+
### Step 3: Set up the state and mutation
178+
179+
In our `vuex/actions.js` file we dispatch an `INCREMENT` mutation but we haven't written how to handle it yet. Let's do that now.
180+
181+
Edit `vuex/store.js`
182+
183+
```js
184+
const state = {
185+
// When the app starts, count is set to 0
186+
count: 0
187+
}
188+
189+
const mutations = {
190+
// A mutation recieves the current state as the first argument
191+
// You can make any modifications you want inside this function
192+
INCREMENT (state, amount) {
193+
state.count = state.count + amount
194+
}
195+
}
196+
```
197+
198+
### Step 4: Get the value into the component
199+
200+
Create a new file called `vuex/getters.js`
201+
202+
```
203+
// This getter is a function which just returns the count
204+
export const getCount = function (state) {
205+
return state.count
206+
}
207+
```
208+
209+
This is a simple function which just returns a subset of the state object which is of interest for us, which is the current count. Now we need to use this getter to actually fetch the data in the component.
210+
211+
Edit `components/Display.vue`
212+
213+
```html
214+
<template>
215+
<div>
216+
<h3>Count is {{ counterValue }}</h3>
217+
</div>
218+
</template>
219+
220+
<script>
221+
import { getCount } from "../vuex/getters"
222+
export default {
223+
vuex: {
224+
getters: {
225+
counterValue: getCount
226+
}
227+
}
228+
}
229+
</script>
230+
```
231+
232+
There's a new object `vuex.getters` which requests `counterValue` to be bound to the getter `getCount`. We've chosen different names to demonstrate that you can use the names that make sense in the context of your component, not necessarily the getter name itself.
233+
234+
You might be wondering, why is it preferable to use a getter instead of using something like `store.state.count` (which doesn't work, but still). While it would be ok here, in a large app:
235+
236+
1. A value may be derived from many other value (think totals, averages, etc.).
237+
2. Many components can use the same getter function.
238+
3. If the value is moved from say `store.count` to `store.counter.value` you'd have to update one getter instead of dozens of components.
239+
240+
These are a few of the benefits of using getters.
241+
242+
### Step 5: Next steps
243+
244+
If you run the application now you will find it behaves as expected.
245+
246+
To further your understanding of vuex you can try implementing the following changes to the app.
247+
248+
* Add a decrement button.
249+
* Install [VueJS Devtools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en) and play with the vuex tools and observe the mutations being applied.
250+
* Add a text input in another component called `IncrementAmount` and enter the amount to increment by. This can be a bit tricky since forms in vuex work slightly differently. Read the [Form Handling](forms.md) section for more details.
251+
252+
253+

docs/en/tutorial/result.png

117 KB
Loading

docs/en/tutorial/vuex_flow.png

601 KB
Loading

0 commit comments

Comments
 (0)