【发布时间】:2018-03-08 15:15:00
【问题描述】:
这是我的 Angular5 应用程序中的一个小数据服务。 我正在尝试从数组中添加/删除项目(实际上是一个 BehaviorSubject)。
data.service.ts
private roomArr_source = new BehaviorSubject<any[]>([]);
current_roomArr = this.roomArr_source.asObservable();
addRoomArr(data: any) {
this.roomArr_source.next(this.roomArr_source.getValue().concat([data]));
}
removeRoomArr(data: any) {
this.roomArr_source.forEach((item, index) => {
if(item === data) { this.roomArr_source.splice(index, 1); }
});
}
addRoomArr 函数有效,但 removeRommArr 无效。 错误是:
error TS2345: Argument of type '(item: any, index: any) => void' is not assignable to parameter of type '(value: any[]) => void'.
error TS2339: Property 'splice' does not exist on type 'BehaviorSubject<any[]>'.
不知何故,我必须将我的 BehaviorSubject 转换为一个数组,以便我可以使用 .forEach()。 我怎样才能做到这一点?
谢谢
【问题讨论】:
-
数据是什么样的?请read this
-
@MikeTung data 是一个简单的字符串;感谢分享