我有多个请求,每个请求的动态参数数组参数的时间间隔都可观察到。因此,如何基于array参数返回主题。由于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();
答案 0 :(得分:1)
请通过以下代码:- 在这里,我提到了其他一些方法,例如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);
});
}
答案 1 :(得分:1)
您可以尝试以下一种,因为我们的实现与您几乎相同:
private requests: Request[];
private observableRequests: BehaviorSubject<Request[]>;
constructor() {
this.requests = new Array<Request>;
this.observableRequests = <BehaviorSubject<Request[]>>new BehaviorSubject([]);
}
get requests() {
return this.observableRequests.asObservable();
}
addRequest(request: Request) {
this.requests.push(request);
this.observableRequests.next(this.requests);
}
每当您调用 addRequest 时,所有请求对象都将返回。 您必须根据需要在该方法中做一些解决方法。