gpt4 book ai didi

angular - 行为主题 : ignore last value if next() didn't triggered

转载 作者:行者123 更新时间:2023-12-05 04:11:01 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 BehaviorSubject 将数据从提供程序传递到页面组件:

工作管理器

  private futureJobsSource = new BehaviorSubject <Array<Job>>([]);
futureJobsUpdate = this.futureJobsSource.asObservable();

源更新的地方:

 getFutureJobs(date: string) {
//
//
this.api.getFutureJobs(request).subscribe((res) => {
console.log(this.TAG + 'getFutureJobs: success: ' + JSON.stringify(res));
this.futureJobsSource.next(res);
}, (err) => {
console.log(this.TAG + 'getFutureJobs: failure: ' + JSON.stringify(err));
this.message.showErrorAlert(err.detail);
});
}

还有我的组件,FutureJobsPage

 setSearchDate() {
DatePicker.show(this.datePickerOptions).then((date) => {
this.jobsManager.getFutureJobs(moment(date).format('MM/DD/YYYY'));
});
}

ionViewDidLoad() {
console.log('ionViewDidLoad FutureJobsPage');
this.subscription = this.jobsManager.futureJobsUpdate.subscribe((res) => {
console.log(this.TAG + 'subscribe: ' + JSON.stringify(res));
this.futureJobs = res;
});
}

ionViewDidLeave() {
console.log(this.TAG + 'ionViewDidLeave');
this.jobsManager.futureJobsUpdate.distinctUntilChanged();
this.subscription.unsubscribe();
this.futureJobs = [];
}

我从搜索中了解到从提供者 BehaviorSubject 传递数据的正确方法是一个最佳解决方案,但如果不是来自 next(),我想忽略最后接收到的值。实现这种行为的最佳做法是什么?

最佳答案

使用Subject代替BehaviorSubject,订阅只会在第一次发出后开始。

行为主题

One of the variants of Subjects is the BehaviorSubject, which has a notion of "the current value". It stores the latest value emitted to its consumers, and whenever a new Observer subscribes, it will immediately receive the "current value" from the BehaviorSubject.

希望这对您有所帮助!

关于angular - 行为主题 : ignore last value if next() didn't triggered,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474093/

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