You can define getter methods in the getters property in the store to compute the state before it is used in the view by the child components. Just like the computed property, the computed result in a getter is reactive, but it is cached and will be updated whenever its dependencies are changed. A getter takes the state as its first argument and getters as the second argument. Let's create some getters and use them in the child component with the following steps:
- Create a store with a state property with a list of items and some getters for accessing these items:
// vuex-sfc/getters/basic/src/entry.js
const store = new Vuex.Store({
state: {
fruits: [
{ name: 'strawberries', type: 'berries' },
{ name: 'orange', type: 'citrus' },
{ name: 'lime', type: 'citrus' }
]
},
getters: {
getCitrus: state => {
return state.fruits.filter(fruit => fruit.type === 'citrus')
...