angular - RXJS 主体和行为主体
问题描述
我在可观察的时间间隔内有多个带有动态参数数组参数的请求。那么如何根据数组参数返回主题。因为其中BehaviorSubject
包含所有数据
初始化主题
getSchedularData: BehaviorSubject < any > = new BehaviorSubject < any > (null);
constructor(private httpClient: HttpClient, ) {
SchedulerStore.select('jobSchedulerState')
.subscribe(response => {
this.schedularDataCollector = response;
});
}
零件
this.schedulerService.startScheduler(this.channelList1)
.subscribe((value) => {
// console.log(value);
// tslint:disable-next-line:forin
for (const keys in value) {
this.schedularData[keys] = value[keys];
}
});
服务
Observable.interval((!this.backChannelEnvironment.schedularInterval) ? 10000 : this.backChannelEnvironment.schedularInterval)
.pipe(
map(() => {
/**
* dispatch request for schedular for requesting http request for channel
*/
this.SchedulerStore.dispatch(new SchedulerAction.GetScheduler(Channels));
})
)
.subscribe(() => {
/**
* get data from schedular store and return it at schedular interval
*/
if (this.schedularDataCollector != null) {
if (JSON.stringify(this.schedularDataCollector['jobScheduler']) !==
JSON.stringify(this.getSchedularData.value)) {
this.getSchedularData.next(this.schedularDataCollector['jobScheduler']);
}
}
});
return this.getSchedularData.asObservable();
解决方案
请浏览以下代码:- 这里我提到了一些其他方法,如 debounceTime() 和 distinctUntilChanged() 根据您的要求进行更改。
@Output() completeMethod: EventEmitter<any> = new EventEmitter();
customEvent: Event;
private yourSubject = new Subject<string>();
constructor(){
this.yourSubject.asObservable().pipe(filter(data => data != null),
debounceTime(1000), distinctUntilChanged()).subscribe(value => {
this.loading = true;
this.completeMethod.emit({
originalEvent: this,
query: value
});
});
}
ngAfterViewInit() {
this.inputEL.nativeElement.addEventListener('keyup', (event) => {
this.customEvent = event;
this.yourSubject.next(event.target.value);
});
}
推荐阅读
- c# - SQL查询将数据插入到asp.net中包含外键的表中
- java - 我想从 GUI 窗口中获取输入并存储
- python - 有没有一种 numpy 风格是按元素相乘两个数组?
- python - Alexnet 中性网络:如何减少网络的消费规模?
- oracle - 具有或不具有唯一列的唯一索引性能
- vue.js - 如何使用 D3 和 Vue 本地化和格式化刻度
- python - 如何在 sla_miss_callback 函数中获取上下文对象
- python - cassandra.cluster.NoHostAvailable: 'Unable to complete the operation against any hosts' 查询大量数据时
- elasticsearch - 弹性搜索中的默认值有什么用
- python - 在 Python Sqlite3 中向同一列添加多个值