gpt4 book ai didi

javascript - 如何清空 Angular 4 中的可观察对象

转载 作者:行者123 更新时间:2023-11-30 14:30:09 25 4
gpt4 key购买 nike

我创建了一个可观察对象,如下所示

  private permissionSubject = new Subject<any>();
permissionObservable$ = this.permissionSubject.asObservable();


constructor( public apiService: ApiService) { }

updatePermissionsData(permissionData){
this.permissionSubject.next(permissionData);
}

getPermissions(){
return this.apiService.get('/getUserPrivileges')
.map(data => data)
}

这里我所做的是,每当我获取数据时,我都会将数据推送到 Observable

Ex: 考虑一个 observable -> [1, 2] 并推送 3,因为它是新数据现在 observable 将变为 [1, 2, 3]

但我想在将 3 推送给它之前从 Observable 中删除 1、2 值。我怎样才能做到这一点?

Observable.empty() 是否可以做到这一点,如果可以,我该如何更新我的代码?

我在 stackoverflow 中看到了很多问题,但没有任何帮助 :-( 这就是为什么我再次问这个问题......

更新代码

订阅可观察

    checkPermissions() {
this.checkPermService.permissionObservable$.subscribe(
// Below one is getting executed for so many times whenever
observable get new data (stream data)
data => {
this.saveMenuItemsOnPermissions(data)
}
)
}

最佳答案

我认为存在对 Observables 工作原理的误解。您的代码中没有缓冲区/内存结构。

您的代码解释

// instance of a subject.
// Subjects don't have memory!! The stream is pushed to subscribers once.
private permissionSubject = new Subject<any>();


// Here you make a restriction on `permissionObservable$`, so it listens, but doesn't publish
permissionObservable$ = this.permissionSubject.asObservable();


// constructor instanciates apiService
constructor( public apiService: ApiService) { }

// each time this function is called, permissionData is pushed through
// permissionObservable and permissionObservable$ subscribers.
updatePermissionsData(permissionData){
this.permissionSubject.next(permissionData);
}

// calls a service and waits for subscription (http call I suppose)
// the map function is useless BTW
getPermissions(){
return this.apiService.get('/getUserPrivileges')
.map(data => data)
}

Observable.empty()

create an Observable that emits no items but terminates normally

Observable.empty() 不是方法!!它是一个可观察对象,其目的是:

  1. 什么都不发出
  2. 挂流

编辑:

如果您只想忽略可观察对象的前 2 个元素,您可以使用 skip 运算符。

跳过运算符:

Skip allows you to ignore the first x emissions from the source. Generally skip is used when you have an observable that always emits certain values on subscription that you wish to ignore. Perhaps those first few aren't needed or you are subscribing to a Replay or BehaviorSubject and do not need to act on the initial values. Reach for skip if you are only concerned about later emissions.

// Below one is getting executed for so many times whenever observable get new data (stream data)
checkPermissions() {
this.checkPermService.permissionObservable$.skip(2)
.subscribe( data => {
this.saveMenuItemsOnPermissions(data)
})
}

有两点需要牢记:

  1. 订阅必须发生在 之前 observable 开始发射
  2. checkPermissions 将忽略订阅期间最先接收到的 2 个元素,但它会采用以下所有其他元素。

关于javascript - 如何清空 Angular 4 中的可观察对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com