Store.mapGetters

为组件创建计算属性以返回 getter 的返回值。详细介绍

参数说明
参数类型必填说明
mapArray | Object如果是对象形式,成员可以是一个函数。function(state: any)
示例
  1. // store.js
  2. import createStore from 'chameleon-store'
  3. const store = createStore({
  4. state: {
  5. todos: [
  6. { id: 1, text: '...', done: true },
  7. { id: 2, text: '...', done: false }
  8. ]
  9. },
  10. getters: {
  11. doneTodos: state => {
  12. return state.todos.filter(todo => todo.done)
  13. }
  14. }
  15. })
  16. export default store
  17. // app.js
  18. import store from './store.js'
  19. class Index {
  20. // ...
  21. computed = {
  22. // 使用对象展开运算符将 getter 混入 computed 对象中
  23. ...store.mapGetters([
  24. 'doneTodosCount',
  25. //'anotherGetter'
  26. ])
  27. }
  28. };
  29. export default new Index();