什么是...在角?什么叫呢?我在想addFlash方法中“ ... flash”的用途是什么,它是array.push()中的参数?
还有在toggleFlash方法中,如果我们只能使用此关键字,为什么会有“ ...” ?
flashs: IFlash[] = [];
flashs$ = new BehaviorSubject<IFlash[]>(this.flashs);
addFlash(flash: IFlash) {
this.flashs.push({
...flash,
show: false,
id: getRandomNumber()
});
}
toggleFlash(id: number) {
const index = this.flashs.findIndex(flash => flash.id === id);
this.flashs = [
...this.flashs.slice(0, index),
{
...this.flashs[index],
show: !this.flashs[index].show
},
...this.flashs.slice(index + 1)
];
this.flashs$.next(this.flashs);
}
...
如果您的代码中是es6 Spread_syntax,...this.flashs
将在您this.flashs
使用它的数组中添加项目,同时{...this.flashs[index]
将给定索引处的对象的属性添加到您使用它的对象中,请阅读以下代码中的注释,以进行进一步的说明
this.flashs = [
...this.flashs.slice(0, index),//slice flashs array and add result items here like obj1,obj2....
{
...this.flashs[index],//get object at given index and add that object properties here like prop1:val,prop2:val,...
show: !this.flashs[index].show
},
...this.flashs.slice(index + 1)
];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句