2017-09-15 57 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