Vuex 允許我們在 store 中定義“getter”
(可以認為是 store 的計算屬性)。就像計算屬性一樣,getter 的返回值會根據它的依賴被緩存起來,且只有當它的依賴值發生了改變才會被重新計算。
const store = new Vuex.Store({
state: {
todos: [
{
id: 1,
text: '...',
done: true
},
{
id: 2,
text: '...',
done: false
}
]
},
getters: {
doneTodos: state => {
return state.todos.filter(todo => todo.done)
}
}
})
(1) 通過屬性訪問
- Getter 會暴露為 store.getters 對象,你可以以屬性的形式訪問這些值:
store.getters.doneTodos // -> [{ id: 1, text: '...', done: true }]
- Getter 也可以接受其他getter 作為第二個參數:
getters: {
// ...
doneTodosCount: (state, getters) => {
return getters.doneTodos.length;
}
}
store.getters.doneTodosCount // 1
- 我們可以很容易的在任何組件中使用它:
computed: {
doneTodosCount (){
return this.$store.getters.doneTodosCount;
}
}
(2) 通過方法訪問
你也可以通過讓 getter 返回一個函數,來實現給 getter 傳參
getters: {
// ...
getTodoById: (state) => (id) => {
return state.todos.find(todo => todo.id === id)
}
}
store.getters.getTodoById(2) // -> { id: 2, text: '...', done: false }
注意,getter 在通過方法訪問時,每次都會去進行調用,而不會緩存結果。
(3) mapGetters 輔助函數
mapGetters 輔助函數僅僅是將 store 中的 getter 映射到局部計算屬性:
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
// 使用對象展開運算符將 getter 混入 computed 對象中
...mapGetters([
'doneTodosCount',
'anotherGetter',
// ...
])
}
}
經過 mapGetters 函數調用后的結果,如下所示:
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
doneTodosCount() {
return this.$store.getters['doneTodosCount']
},
anotherGetter() {
return this.$store.getters['anotherGetter']
}
}
}
再看一個參數 mapGetters 參數是對象的例子:
computed: mapGetters({
// 映射 this.doneCount 到 store.getters.doneTodosCount
doneCount: 'doneTodosCount'
})
經過 mapGetters 函數調用后的結果,如下所示:
computed: {
doneCount() {
return this.$store.getters['doneTodosCount']
}
}