2017-09-15 46 views
2

这三个点究竟意味着什么,以及为什么我需要它们?角度@NGRX中三个点的含义

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) 
} 
} 

回答

5

的三个点都称为spread operator从打字稿(也来自ES7)。

spread操作符返回数组的所有元素。因为它编译此

let myArr = [1, 2, 3]; 
return [1, 2, 3]; 
//is the same as: 
return [...myArr]; 

这是大多只是语法糖:

func(...args); 

这样:

func.apply(null, args); 

你的情况,这被编译,就像您写单独的每个元素这个:

return [...state, action.payload]; 
//gets compiled to this: 
return state.concat([action.payload]); 
+2

这是es7的原因它在Object not array上工作。 – alexKhymenko

+0

@alexKhymenko感谢您的通知:) – Wernerson

1

...spread operator)的工作原理是从索引0每个值返回到索引length-1