角@NGRX中的三个点是什么意思 [英] Angular what's meaning of three dots in @NGRX

查看:100
本文介绍了角@NGRX中的三个点是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这三个点到底是什么意思,为什么我需要它们?

what does mean exactly these three dots and why 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 )
}
}

推荐答案

这三个点称为传播算子来自Typescript(也来自 ES7 ).

The three dots are known as the spread operator from Typescript (also from ES7).

spread运算符返回数组的所有元素.就像您要分别编写每个元素一样:

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]);

这篇关于角@NGRX中的三个点是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆