Sometimes we may need to get derived state based on store state, like filtering for a specific prop.
This can be done through the `getters`:
...
...
@@ -191,6 +228,62 @@ The store should be included in the main component of your application:
};
```
### Communicating with the Store
```javascript
<script>
import{mapActions,mapState,mapGetters}from'vuex';
importstorefrom'./store';
exportdefault{
store,
computed:{
...mapGetters([
'getUsersWithPets'
]),
...mapState([
'isLoading',
'users',
'error',
]),
},
methods:{
...mapActions([
'fetchUsers',
'addUser',
]),
onClickAddUser(data){
this.addUser(data);
}
},
created(){
this.fetchUsers()
.catch(()=>{
// TODO - Decide where to handle the `createFlash`
})
}
}
</script>
<template>
<ul>
<liv-if="isLoading">
Loading...
</li>
<liv-else-if="error">
{{error}}
</li>
<li
v-else
v-for="user in users"
:key="user.id"
>
{{user}}
</li>
</ul>
</template>
```
### Vuex Gotchas
1. Do not call a mutation directly. Always use an action to commit a mutation. Doing so will keep consistency through out the application. From Vuex docs: