mapState

function mapState(stateList) {
  let obj = {};
  for (let i = 0; i < stateList.length; i++) {
    let stateName = stateList[i];
    obj[stateName] = function () {
      return this.$store.state[stateName];
    };
  }
  return obj;
}

mapGetters

function mapGetters(gettersList) {
  let obj = {};
  for (let i = 0; i < gettersList.length; i++) {
    let getterName = gettersList[i];
    obj[getterName] = function () {
      return this.$store.getters[getterName];
    };
  }
  return obj;
}

mapMutations

function mapMutations(mutationList) {
  let obj = {};
  for (let i = 0; i < mutationList.length; i++) {
    obj[mutationList[i]] = function (payload) {
      this.$store.commit(mutationList[i], payload);
    };
  }
  return obj;
}

mapActions

function mapActions(actionList) {
  let obj = {};
  for (let i = 0; i < actionList.length; i++) {
    obj[actionList[i]] = function (payload) {
      this.$store.dispatch(actionList[i], payload);
    };
  }
  return obj;
}
打赏作者
您的打赏是我前进的动力
微信
支付宝
评论

中午好👏🏻,我是 ✍🏻   疯狂 codding 中...

粽子

这有关于前端开发的技术文档和你分享。

相信你可以在这里找到对你有用的知识和教程。

了解更多

目录

  1. 1. mapState
  2. 2. mapGetters
  3. 3. mapMutations
  4. 4. mapActions