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
* add description of the 2nd and 3rd arguments of getters
* add example test code of getters
* tweak description of module getters
* fix small grammatical error
Copy file name to clipboardExpand all lines: docs/en/getters.md
+18-3Lines changed: 18 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ computed: {
13
13
14
14
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
15
16
-
Vuex allows us to define "getters" in the store (think of them as computed properties for stores):
16
+
Vuex allows us to define "getters" in the store (think of them as computed properties for stores). Getters will receive the state as their 1st argument:
17
17
18
18
```js
19
19
conststore=newVuex.Store({
@@ -24,15 +24,30 @@ const store = new Vuex.Store({
24
24
]
25
25
},
26
26
getters: {
27
-
doneTodosCount:state=> {
28
-
returnstate.todos.filter(todo=>todo.done).length
27
+
doneTodos:state=> {
28
+
returnstate.todos.filter(todo=>todo.done)
29
29
}
30
30
}
31
31
})
32
32
```
33
33
34
34
The getters will be exposed on the `store.getters` object:
Copy file name to clipboardExpand all lines: docs/en/modules.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -69,6 +69,19 @@ const moduleA = {
69
69
}
70
70
```
71
71
72
+
Also, inside module getters, the root state will be exposed as their 3rd argument:
73
+
74
+
```js
75
+
constmoduleA= {
76
+
// ...
77
+
getters: {
78
+
sumWithRootCount (state, getters, rootState) {
79
+
returnstate.count+rootState.count
80
+
}
81
+
}
82
+
}
83
+
```
84
+
72
85
### Namespacing
73
86
74
87
Note that actions, mutations and getters inside modules are still registered under the **global namespace** - this allows multiple modules to react to the same mutation/action type. You can namespace the module assets yourself to avoid name clashing by prefixing or suffixing their names. And you probably should if you are writing a reusable Vuex module that will be used in unknown environments. For example, we want to create a `todos` module:
If your mutations and actions are written properly, the tests should have no direct dependency on Browser APIs after proper mocking. Thus you can simply bundle the tests with Webpack and run it directly in Node. Alternatively, you can use `mocha-loader` or Karma + `karma-webpack` to run the tests in real browsers.
0 commit comments