You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/tutorial.md
+46-33Lines changed: 46 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
# Tutorial
2
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.
3
+
Let's build a very simple app using vuex to understand how to use it. For this example, we're building an app where you press a button, and it increments a counter.
4
4
5
5

6
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:
7
+
We are using this simple example to explain the concepts, and the problems vuex aims to solve - how to manage a large app which uses several components. Consider if this example used three components:
8
8
9
9
### `components/App.vue`
10
10
11
-
The main app which contains two other child components:
11
+
The root component, which contains two other child components:
12
12
13
13
*`Display` to display the current counter value.
14
14
*`Increment` which is a button to increment the current value.
@@ -23,21 +23,21 @@ The main app which contains two other child components:
23
23
24
24
<script>
25
25
26
-
importDisplayfrom"./Display.vue"
27
-
importIncrementfrom"./Increment.vue"
26
+
importDisplayfrom'./Display.vue'
27
+
importIncrementfrom'./Increment.vue'
28
28
29
29
exportdefault {
30
30
components: {
31
-
Display,
32
-
Increment
31
+
Display: Display,
32
+
Increment: Increment
33
33
}
34
34
}
35
35
</script>
36
36
```
37
37
38
38
### `components/Display.vue`
39
39
40
-
```
40
+
```html
41
41
<template>
42
42
<div>
43
43
<h3>Count is 0</h3>
@@ -52,7 +52,7 @@ export default {
52
52
53
53
### `components/Increment.vue`
54
54
55
-
```
55
+
```html
56
56
<template>
57
57
<div>
58
58
<button>Increment +1</button>
@@ -65,23 +65,23 @@ export default {
65
65
</script>
66
66
```
67
67
68
-
### Challenges
68
+
### Challenges without vuex
69
69
70
-
*`Increment` and `Display` aren't aware of each other and cannot pass messages to each other.
70
+
*`Increment` and `Display` aren't aware of each other, and cannot pass messages to each other.
71
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
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
73
74
74
### Vuex "flow"
75
75
76
76
These are the steps that take place in order:
77
77
78
78

79
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.
80
+
This might seem a little excessive for incrementing a counter. But do note that these concepts work well in larger applications, improving maintainability and making your app easier to debug and improve in the long run. So let's modify our code to use vuex.
81
81
82
82
### Step 1: Add a store
83
83
84
-
First, install vuex via npm:
84
+
The store holds the data for the app. All components read the data from the store. Before we begin, install vuex via npm:
85
85
86
86
```
87
87
$ npm install --save vuex
@@ -120,26 +120,37 @@ We need to make our app aware of this store. To do this we simply need to modify
120
120
Edit `components/App.vue` and add the store.
121
121
122
122
```js
123
-
importDisplayfrom"./Display.vue"
124
-
importIncrementfrom"./IncrementButton.vue"
125
-
importstorefrom'../vuex/store'// import the store
123
+
importDisplayfrom'./Display.vue'
124
+
importIncrementfrom'./IncrementButton.vue'
125
+
importstorefrom'../vuex/store'// import the store we just created
126
126
127
127
exportdefault {
128
128
components: {
129
-
Display,
130
-
Increment
129
+
Display: Display,
130
+
Increment: Increment
131
131
},
132
132
store: store // make this and all child components aware of the new store
133
133
}
134
134
```
135
135
136
+
> **Tip**: With ES6 and babel you can write it as
137
+
>
138
+
> components: {
139
+
> Display,
140
+
> Increment,
141
+
> },
142
+
> store
143
+
136
144
### Step 2: Set up the action
137
145
146
+
The action is a function which is called from the component. Action functions can trigger updates in the store by dispatching the right mutation. An action can also talk to HTTP backends and read other data from the store before dispatching updates.
147
+
138
148
Create a new file in `vuex/actions.js` with a single function `incrementCounter`
139
149
140
-
```
150
+
```js
141
151
// An action will recieve the store as the first argument.
142
152
// Since we are only interested in the dispatch (and optionally the state)
153
+
// We can pull those two parameters using the ES6 destructuring feature
143
154
exportconstincrementCounter=function ({ dispatch, state }) {
144
155
dispatch('INCREMENT', 1)
145
156
}
@@ -150,12 +161,12 @@ And let's call the action from our `components/Increment.vue` component.
150
161
```
151
162
<template>
152
163
<div>
153
-
<button @click="increment">Increment +1</button>
164
+
<button @click='increment'>Increment +1</button>
154
165
</div>
155
166
</template>
156
167
157
168
<script>
158
-
import { incrementCounter } from "../vuex/actions"
169
+
import { incrementCounter } from '../vuex/actions'
159
170
export default {
160
171
vuex: {
161
172
actions: {
@@ -199,14 +210,17 @@ const mutations = {
199
210
200
211
Create a new file called `vuex/getters.js`
201
212
202
-
```
213
+
```js
203
214
// This getter is a function which just returns the count
204
-
export const getCount = function (state) {
215
+
// With ES6 you can also write it as:
216
+
// export const getCount = state => state.count
217
+
218
+
exportfunctiongetCount (state) {
205
219
returnstate.count
206
220
}
207
221
```
208
222
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.
223
+
This function returns a part of the state object which is of interest - the current count. We can now use this getter inside the component.
// note that you're passing the function itself, and not the value 'getCount()'
225
240
counterValue: getCount
226
241
}
227
242
}
@@ -231,23 +246,21 @@ export default {
231
246
232
247
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
248
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:
249
+
You might be wondering - why did we choose to use a getter instead of directly accessing the value from the state. This concept is more of a best practice, and is more applicable to a larger app, which presents several distinct advantages:
235
250
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.
251
+
1.We may want to define getters with computed values (think totals, averages, etc.).
252
+
2. Many components in a larger app can use the same getter function.
238
253
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
254
240
255
These are a few of the benefits of using getters.
241
256
242
257
### Step 5: Next steps
243
258
244
-
If you run the application now you will find it behaves as expected.
259
+
If you run the application, now you will find it behaves as expected.
245
260
246
-
To further your understanding of vuex you can try implementing the following changes to the app.
261
+
To further your understanding of vuex, you can try implementing the following changes to the app, as an exercise.
247
262
248
263
* Add a decrement button.
249
264
* 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
265
* 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.
0 commit comments