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
If more than one component needs to make use of this, we have to either duplicate the function, or extract it into a shared helper and import it in multiple places - both are less than ideal.
15
+
16
+
Vuex allows us to define "getters" in the store (think of them as computed properties for stores):
17
+
18
+
```js
19
+
conststore=newVuex.Store({
20
+
state: {
21
+
todos: [
22
+
{ id:1, text:'...', done:true },
23
+
{ id:2, text:'...', done:false }
24
+
]
25
+
},
26
+
getters: {
27
+
doneTodosCount:state=> {
28
+
returnstate.todos.filter(todo=>todo.done).length
29
+
}
30
+
}
31
+
})
32
+
```
33
+
34
+
The getters will be exposed on the `store.getters` object:
35
+
36
+
```js
37
+
store.getters.doneTodosCount// -> 1
38
+
```
39
+
40
+
We can now easily make use of it inside any component:
41
+
42
+
```js
43
+
computed: {
44
+
doneTodosCount () {
45
+
returnthis.$store.getters.doneTodosCount
46
+
}
47
+
}
48
+
```
49
+
50
+
### The `mapGetters` Helper
51
+
52
+
The `mapGetters` helper simply maps store getters to local computed properties:
53
+
54
+
```js
55
+
import { mapState } from'vuex'
56
+
57
+
exportdefault {
58
+
// ...
59
+
computed: {
60
+
// mix the getters into computed with object spread operator
61
+
...mapGetters([
62
+
'doneTodosCount',
63
+
'anotherGetter',
64
+
// ...
65
+
])
66
+
}
67
+
}
68
+
```
69
+
70
+
If you want to map a getter to a different name, use an object:
71
+
72
+
```js
73
+
mapGetters({
74
+
// map this.doneCount to store.getters.doneTodosCount
Copy file name to clipboardExpand all lines: docs/en/getting-started.md
+17-30Lines changed: 17 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,50 +4,37 @@ At the center of every Vuex application is the **store**. A "store" is basically
4
4
5
5
1. Vuex stores are reactive. When Vue components retrieve state from it, they will reactively and efficiently update if the store's state changes.
6
6
7
-
2. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly dispatching **mutations**. This makes every state change easily track-able, and enables tooling that helps us better understand our applications.
7
+
2. You cannot directly mutate the store's state. The only way to change a store's state is by explicitly **committing mutations**. This ensures every state change leaves a track-able record, and enables tooling that helps us better understand our applications.
8
8
9
9
### The Simplest Store
10
10
11
-
> **NOTE:** We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, [you should](https://babeljs.io/docs/learn-es2015/)! The doc also assumes you are already familiar with the concepts discussed in [Building Large-Scale Apps with Vue.js](http://vuejs.org/guide/application.html).
11
+
> **NOTE:** We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, [you should](https://babeljs.io/docs/learn-es2015/)!
12
12
13
-
Creating a Vuexstore is pretty straightforward - just provide an initial state object, and some mutations:
13
+
After [installing](installation.md) Vuex, let's create a store. It is pretty straightforward - just provide an initial state object, and some mutations:
14
14
15
15
```js
16
-
importVuexfrom'vuex'
17
-
18
-
conststate= {
19
-
count:0
20
-
}
21
-
22
-
constmutations= {
23
-
INCREMENT (state) {
24
-
state.count++
16
+
// Make sure to call Vue.use(Vuex) first if using a module system
17
+
18
+
conststore=newVuex.Store({
19
+
state: {
20
+
count:0
21
+
},
22
+
mutations: {
23
+
INCREMENT (state) {
24
+
state.count++
25
+
}
25
26
}
26
-
}
27
-
28
-
exportdefaultnewVuex.Store({
29
-
state,
30
-
mutations
31
27
})
32
28
```
33
29
34
-
Now, you can access the state object as `store.state`, and trigger a mutation by dispatching its name:
30
+
Now, you can access the state object as `store.state`, and trigger a state change with the `store.commit` method:
35
31
36
32
```js
37
-
store.dispatch('INCREMENT')
33
+
store.commit('INCREMENT')
38
34
39
35
console.log(store.state.count) // -> 1
40
36
```
41
37
42
-
If you prefer object-style dispatching, you can also do this:
43
-
44
-
```js
45
-
// same effect as above
46
-
store.dispatch({
47
-
type:'INCREMENT'
48
-
})
49
-
```
50
-
51
-
Again, the reason we are dispatching a mutation instead of changing `store.state.count` directly, is because we want to explicitly track it. This simple convention makes your intention more explicit, so that you can reason about state changes in your app better when reading the code. In addition, this gives us the opportunity to implement tools that can log every mutation, take state snapshots, or even perform time travel debugging.
38
+
Again, the reason we are committing a mutation instead of changing `store.state.count` directly, is because we want to explicitly track it. This simple convention makes your intention more explicit, so that you can reason about state changes in your app better when reading the code. In addition, this gives us the opportunity to implement tools that can log every mutation, take state snapshots, or even perform time travel debugging.
52
39
53
-
Now this is just the simplest possible example of what a store is. But Vuex is more than just the store. Next, we will discuss some core concepts in depth: [State](state.md), [Mutations](mutations.md) and [Actions](actions.md).
40
+
Next, let's see [how to use the State inside Vue components](state.md).
Copy file name to clipboardExpand all lines: docs/en/mutations.md
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,6 @@
3
3
Vuex mutations are essentially events: each mutation has a **name** and a **handler**. The handler function will receive the state as the first argument:
0 commit comments