[IMP] store: getters

This commit is contained in:
Alexandre Kühn
2019-05-08 17:10:12 +02:00
committed by Géry Debongnie
parent d928927da8
commit 147f6fced7
3 changed files with 225 additions and 19 deletions
+13 -11
View File
@@ -83,7 +83,7 @@ object, or modifying an array with the `arr[i] = newValue` syntax). See the
### Mutations
Mutations are the only way to modify the state. Changing the state outside a
mutation is not allowed (and should throw an error). Mutations as synchronous.
mutation is not allowed (and should throw an error). Mutations are synchronous.
### Actions
@@ -93,7 +93,7 @@ in an action.
```js
const actions = {
login({commit}) {
async login({commit}) {
commit('setLoginState', 'pending');
try {
const loginInfo = await doSomeRPC('/login/', 'someinfo');
@@ -128,18 +128,20 @@ transform the data contained in the store.
```js
const getters = {
getPost({state}) {
return function (id) {
const post = state.posts.find(p => p.id === id);
const author = state.authors.find(a => a.id = post.id);
return {
id,
author,
content: post.content
};
getPost({state}, id) {
const post = state.posts.find(p => p.id === id);
const author = state.authors.find(a => a.id = post.id);
return {
id,
author,
content: post.content
};
},
};
// somewhere else
const post = store.getters.getPost(id);
```
### Connecting a component