首页 > 其他 > 详细

手写vuex的mapGetters

时间:2020-06-14 13:35:36      阅读:55      评论:0      收藏:0      [点我收藏+]
<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>

 

手写vuex的mapGetters

原文:https://www.cnblogs.com/zhuxingqing/p/13124309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!