手写vuex的mapGetters

<script>
  import { mapGetters } from 'vuex'

  const getters = {
    a: () => 1,
    b: () => 2
  }
  function fn (keys) {
    const data = {}
    keys.forEach(key => {
      // if (getters.hasOwnProperty(key)) {
      if (Object.prototype.hasOwnProperty.call(getters, key)) {
        data[key] = getters[key]
      }
    })
    return data
  }
  export default {
    computed: {
      ...mapGetters(['test']),
      ...fn(['a', 'b', 'c'])
    },
    mounted () {
      this.$store.dispatch('setTest', 9).then(() => {
        console.log(this.test)
        console.log(this.a, this.b, this.c)
      })
    }
  }
</script>
原文地址:https://www.cnblogs.com/zhuxingqing/p/13124309.html