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
+22-23Lines changed: 22 additions & 23 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 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.
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 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:
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 root component, 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.
@@ -65,7 +65,7 @@ export default {
65
65
</script>
66
66
```
67
67
68
-
### Challenges without vuex
68
+
### Challenges without Vuex
69
69
70
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.
@@ -77,11 +77,11 @@ 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, improving maintainability and making your app easier to debug and improve in the long run. So let's modify our code 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
-
The store holds the data for the app. All components read the data from the store. Before we begin, 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
@@ -93,10 +93,10 @@ Create a new file in `vuex/store.js`
93
93
importVuefrom'vue'
94
94
importVuexfrom'vuex'
95
95
96
-
// Make vue aware of vuex
96
+
// Make vue aware of Vuex
97
97
Vue.use(Vuex)
98
98
99
-
//We create an object to hold the initial state when
99
+
//Create an object to hold the initial state when
100
100
// the app starts up
101
101
conststate= {
102
102
// TODO: Set up our initial state
@@ -107,7 +107,7 @@ const mutations = {
107
107
// TODO: set up our mutations
108
108
}
109
109
110
-
//We combine the intial state and the mutations to create a vuex store.
110
+
//Combine the initial state and the mutations to create a Vuex store.
111
111
// This store can be linked to our app.
112
112
exportdefaultnewVuex.Store({
113
113
state,
@@ -133,8 +133,8 @@ export default {
133
133
}
134
134
```
135
135
136
-
> **Tip**: With ES6 and babel you can write it as
137
-
>
136
+
> **Tip**: With ES6 and babel you can write it as
137
+
>
138
138
> components: {
139
139
> Display,
140
140
> Increment,
@@ -145,20 +145,20 @@ export default {
145
145
146
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
147
148
-
Create a new file in `vuex/actions.js` with a single function `incrementCounter`
148
+
Create a new file in `vuex/actions.js` with a single function `incrementCounter`.
149
149
150
150
```js
151
151
// An action will receive the store as the first argument.
152
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
153
+
//we can pull those two parameters using the ES6 destructuring feature
154
154
exportconstincrementCounter=function ({ dispatch, state }) {
155
155
dispatch('INCREMENT', 1)
156
156
}
157
157
```
158
158
159
159
And let's call the action from our `components/Increment.vue` component.
160
160
161
-
```
161
+
```html
162
162
<template>
163
163
<div>
164
164
<button@click='increment'>Increment +1</button>
@@ -179,10 +179,10 @@ export default {
179
179
180
180
Notice some interesting things about what we just added.
181
181
182
-
1. We have a new object `vuex.actions` which includes the new action
182
+
1. We have a new object `vuex.actions` which includes the new action.
183
183
2. We didn't specify which store, object, state, etc. Vuex wires everything up for us.
184
-
3. We can call the action either by using `this.increment()` in any method.
185
-
4. We can also call the action using the `@click` parameter making `increment` like any regular vue component method.
184
+
3. We can call the action by using `this.increment()` in any method.
185
+
4. We can also call the action using the `@click` parameter making `increment` like any regular Vue component method.
186
186
5. The action is called `incrementCounter` but we can use any name which is appropriate.
187
187
188
188
### Step 3: Set up the state and mutation
@@ -198,7 +198,7 @@ const state = {
198
198
}
199
199
200
200
constmutations= {
201
-
// A mutation recieves the current state as the first argument
201
+
// A mutation receives the current state as the first argument
202
202
// You can make any modifications you want inside this function
203
203
INCREMENT (state, amount) {
204
204
state.count=state.count+ amount
@@ -252,15 +252,14 @@ You might be wondering - why did we choose to use a getter instead of directly a
252
252
2. Many components in a larger app can use the same getter function.
253
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.
254
254
255
-
These are a few of the benefits of using getters.
255
+
These are a few of the benefits of using getters.
256
256
257
257
### Step 5: Next steps
258
258
259
259
If you run the application, now you will find it behaves as expected.
260
260
261
-
To further your understanding of vuex, you can try implementing the following changes to the app, as an exercise.
261
+
To further your understanding of Vuex, you can try implementing the following changes to the app, as an exercise.
262
262
263
263
* Add a decrement button.
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.
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.
266
-
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.
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