从 store 中的 state 中派生出一些状态;
getters: {
// ...
doneTodosCount (state, getters) {
return getters.doneTodos.length
}
}
computed: {
doneTodosCount () {
return this.$store.getters.doneTodosCount
}
}
mapGetters 辅助函数
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters([
'doneTodosCount',
'anotherGetter',
// ...
])
}
}
本文介绍了如何在Vue应用中使用Vuex的状态管理机制,通过getters创建辅助计算属性doneTodosCount,并利用mapGetters函数将其混入到computed对象中,以实现组件级别的状态复用。

被折叠的 条评论
为什么被折叠?



