首页 > 解决方案 > 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();

标签: angularrxjs5rxjs6

解决方案


请浏览以下代码:- 这里我提到了一些其他方法,如 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);
        });
      }

推荐阅读