Angular 这三个点在@NGRX 中的含义是什么 [英] Angular what's the meaning of these three dots in @NGRX
本文介绍了Angular 这三个点在@NGRX 中的含义是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这三个点的确切含义是什么,我为什么需要它们?
What do these three dots mean exactly, and why do I need them?
export function leadReducer(state: Lead[]= [], action: Action {
switch(action.type){
case ADD_LEAD:
return [...state, action.payload];
case REMOVE_LEAD:
return state.filter(lead => lead.id !== action.payload.id )
}
}
推荐答案
这三个点被称为 spread operator 来自 Typescript(也来自 ES7).
The three dots are known as the spread operator from Typescript (also from ES7).
展开运算符返回数组的所有元素.就像您将单独编写每个元素一样:
The spread operator return all elements of an array. Like you would write each element separately:
let myArr = [1, 2, 3];
return [1, 2, 3];
//is the same as:
return [...myArr];
这主要是编译时的语法糖:
This is mostly just syntactic sugar as it compiles this:
func(...args);
为此:
func.apply(null, args);
在你的情况下,它被编译成这样:
In your case this gets compiled to this:
return [...state, action.payload];
//gets compiled to this:
return state.concat([action.payload]);
这篇关于Angular 这三个点在@NGRX 中的含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文