vue中mapGetters和...mapGetters

  

根据文档介绍 https://vuex.vuejs.org/zh-cn/... 和看了 http://www.imooc.com/article/...

  export default {
    computed: {
      ...mapGetters([
        'singer'
      ]),
    }
    ....
  }  

最终会转换成

  export default {
    computed: {
      singer() {
        return this.$store.state.singer
      }
    }
    ...
  }  

扩展运算符...可以将数组转换成参数序列,但是这个地方是在函数前面使用了,根据源码mapGetters函数返回的这个对象。
所以不太明白

 

vuex.common.js里面定义的mapGetters:

var mapGetters = normalizeNamespace(function (namespace, getters) {
var res = {};
normalizeMap(getters).forEach(function (ref) {

var key = ref.key;
var val = ref.val;

val = namespace + val;
res[key] = function mappedGetter () {
  if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
    return
  }
  if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
    console.error(("[vuex] unknown getter: " + val));
    return
  }
  return this.$store.getters[val]
};
// mark vuex getter for devtools
res[key].vuex = true;

});
return res
});

参考文档: https://www.cnblogs.com/websmile/p/8328138.html

 
 
 
原文地址:https://www.cnblogs.com/guiyishanren/p/11208221.html