Store.mapState

为组件创建计算属性以返回 chameleon store 中的状态。详细介绍

参数说明
参数类型必填说明
mapArray | Object如果是对象形式,成员可以是一个函数。function(state: any)
示例
  1. // store.js
  2. import createStore from 'chameleon-store'
  3. const store = createStore({
  4. state: {
  5. count: 0
  6. },
  7. mutations: {
  8. increment (state) {
  9. state.count++
  10. }
  11. }
  12. })
  13. export default store
  14. // app.js
  15. import store from './store.js'
  16. class Index {
  17. // ...
  18. computed = store.mapState({
  19. // 箭头函数可使代码更简练
  20. count: state => state.count,
  21. // 传字符串参数 'count' 等同于 `state => state.count`
  22. countAlias: 'count',
  23. // 为了能够使用 `this` 获取局部状态,必须使用常规函数
  24. countPlusLocalState (state) {
  25. return state.count + this.localCount
  26. }
  27. })
  28. };
  29. export default new Index();