Skip to content

Commit d5737d2

Browse files
committed
Improve tutorial based on suggestions
1 parent b57e84b commit d5737d2

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

docs/en/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
- [What is Vuex?](intro.md)
66
- [Getting Started](getting-started.md)
7+
- [Tutorial](tutorial.md)
78
- Core Concepts
89
- [State and Getters](state.md)
910
- [Mutations](mutations.md)

docs/en/tutorial.md

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Tutorial
22

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.
44

55
![End Result](tutorial/result.png)
66

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:
88

99
### `components/App.vue`
1010

11-
The main app which contains two other child components:
11+
The root component, which contains two other child components:
1212

1313
* `Display` to display the current counter value.
1414
* `Increment` which is a button to increment the current value.
@@ -23,21 +23,21 @@ The main app which contains two other child components:
2323

2424
<script>
2525
26-
import Display from "./Display.vue"
27-
import Increment from "./Increment.vue"
26+
import Display from './Display.vue'
27+
import Increment from './Increment.vue'
2828
2929
export default {
3030
components: {
31-
Display,
32-
Increment
31+
Display: Display,
32+
Increment: Increment
3333
}
3434
}
3535
</script>
3636
```
3737

3838
### `components/Display.vue`
3939

40-
```
40+
```html
4141
<template>
4242
<div>
4343
<h3>Count is 0</h3>
@@ -52,7 +52,7 @@ export default {
5252

5353
### `components/Increment.vue`
5454

55-
```
55+
```html
5656
<template>
5757
<div>
5858
<button>Increment +1</button>
@@ -65,23 +65,23 @@ export default {
6565
</script>
6666
```
6767

68-
### Challenges
68+
### Challenges without vuex
6969

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.
7171
* `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.
7373

7474
### Vuex "flow"
7575

7676
These are the steps that take place in order:
7777

7878
![Vuex Flow](tutorial/vuex_flow.png)
7979

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.
8181

8282
### Step 1: Add a store
8383

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:
8585

8686
```
8787
$ 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
120120
Edit `components/App.vue` and add the store.
121121

122122
```js
123-
import Display from "./Display.vue"
124-
import Increment from "./IncrementButton.vue"
125-
import store from '../vuex/store' // import the store
123+
import Display from './Display.vue'
124+
import Increment from './IncrementButton.vue'
125+
import store from '../vuex/store' // import the store we just created
126126

127127
export default {
128128
components: {
129-
Display,
130-
Increment
129+
Display: Display,
130+
Increment: Increment
131131
},
132132
store: store // make this and all child components aware of the new store
133133
}
134134
```
135135

136+
> **Tip**: With ES6 and babel you can write it as
137+
>
138+
> components: {
139+
> Display,
140+
> Increment,
141+
> },
142+
> store
143+
136144
### Step 2: Set up the action
137145

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+
138148
Create a new file in `vuex/actions.js` with a single function `incrementCounter`
139149

140-
```
150+
```js
141151
// An action will recieve the store as the first argument.
142152
// Since we are only interested in the dispatch (and optionally the state)
153+
// We can pull those two parameters using the ES6 destructuring feature
143154
export const incrementCounter = function ({ dispatch, state }) {
144155
dispatch('INCREMENT', 1)
145156
}
@@ -150,12 +161,12 @@ And let's call the action from our `components/Increment.vue` component.
150161
```
151162
<template>
152163
<div>
153-
<button @click="increment">Increment +1</button>
164+
<button @click='increment'>Increment +1</button>
154165
</div>
155166
</template>
156167
157168
<script>
158-
import { incrementCounter } from "../vuex/actions"
169+
import { incrementCounter } from '../vuex/actions'
159170
export default {
160171
vuex: {
161172
actions: {
@@ -199,14 +210,17 @@ const mutations = {
199210

200211
Create a new file called `vuex/getters.js`
201212

202-
```
213+
```js
203214
// 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+
export function getCount (state) {
205219
return state.count
206220
}
207221
```
208222

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.
210224

211225
Edit `components/Display.vue`
212226

@@ -218,10 +232,11 @@ Edit `components/Display.vue`
218232
</template>
219233

220234
<script>
221-
import { getCount } from "../vuex/getters"
235+
import { getCount } from '../vuex/getters'
222236
export default {
223237
vuex: {
224238
getters: {
239+
// note that you're passing the function itself, and not the value 'getCount()'
225240
counterValue: getCount
226241
}
227242
}
@@ -231,23 +246,21 @@ export default {
231246

232247
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.
233248

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:
235250

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.
238253
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.
239254

240255
These are a few of the benefits of using getters.
241256

242257
### Step 5: Next steps
243258

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.
245260

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.
247262

248263
* Add a decrement button.
249264
* 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.
250265
* 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.
251266

252-
253-

0 commit comments

Comments
 (0)