【发布时间】:2019-05-31 10:50:12
【问题描述】:
我刚刚浏览了 habitica 代码(一个 vue.js 应用程序),并遇到了下面的传播运算符用法,例如 HERE。在计算里面你有以下内容:
computed: {
...mapState({
user: 'user.data',
castingSpell: 'spellOptions.castingSpell',
isRunningYesterdailies: 'isRunningYesterdailies',
}),
}
现在我似乎从来没有将扩展运算符与函数结合使用,不确定这里发生了什么,mapState 发生在另一个文件中的函数,看起来像这样:
export function mapState (states) {
const res = {};
normalizeMap(states).forEach(({ key, val }) => {
res[key] = function mappedState () {
return typeof val === 'function' ?
val.call(this, this.$store.state, this.$store.getters) :
get(this.$store.state, val);
};
});
return res;
}
当与Array 或Object 一起使用时,我了解展开运算符,但函数到底发生了什么?
【问题讨论】:
-
函数返回一个对象,这就是展开运算符的用途。
-
函数正在返回一个对象,该对象最终会传播到您的
computed对象上
标签: javascript vue.js ecmascript-6