链1可观察的结果以及最终const的可观察流

时间:2019-01-28 20:22:20

标签: angular typescript angular6 rxjs6

我在一项服务中获得了5个行为主体,在这些行为主体中,我从其他组件中收集信息,最后将它们组合到最终组件中的最后一个对象中。 我的问题是,我无法获得收集所有元素的最终对象。

详细信息是,在第一个组件中,用户选择要采取的两条道路之一,因此,在最终组件中-在我选择所有内容之前,我需要查看用户选择了什么,只是吃掉可观察到的(forthObsOne or forthObsTwo)而不是他的未选兄弟。

my-service.service.ts

private firstObs = new BehaviorSubject(new Class1());
private secondObs = new BehaviorSubject(new Class2());
private thirdObs = new BehaviorSubject(new Class5());

//this are the two brother component to be chosen from
private forthObsOne = new BehaviorSubject(new Class3());
private forthObsTwo = new BehaviorSubject(new Class3);

// This is the obs for the option component where i choose if
// i take the forthObsOne or the forthObsTwo road
private optionObs = new BehaviorSubject(new Class6());

// Now i project those subjects as an observable like the docs recommendation
firstObs$ = this.firstObs.asObservable();
secondObs$ = this.secondObs.asObservable();
thirdObs$ = this.thirdObs.asObservable();
forthObsOne$ = this.forthObsOne.asObservable();
forthObsTwo$ = this.forthObsTwo.asObservable();
optionObs$ = this.optionObs.asObservable();

// Methods for every subject to pass the data from where they are called
firstObsNext(data: Class1) {
  this.firstObs.next(data)
} 

secondObsNext(data: Class1) {
  this.secondObs.next(data)
}

thirdObsNext(data: Class1) {
  this.thirdObs.next(data)
}

forthObsOneNext(data: Class1) {
  this.forthObsOne.next(data)
}

forthObsTwoNext(data: Class1) {
  this.forthObsTwo.next(data)
}

optionObsNext(data: Class1) {
  this.optionObs.next(data)
}

// The latest thing i tried is with combielatest where i combine 
// every observable and try to assign the data to a new big object.

我尝试过首先订阅optionObs,并做出if / else陈述以走forthObsOneforthObsTwo之路。但是最后我得到了一些未定义的元素。 我尝试通过超时来延迟所有内容,但我认为这更像是黑客行为,但不是很专业

最后,我尝试使用combinelatest,但失败了,因为我认为我不太了解它(我看过视频和文档,但仍然不能将其应用于这个特定问题)。

我希望得到一个最终的对象,其中包含我通过组件收集的所有内容。

我希望它足够清楚。谢谢。

更新 我正在尝试使用我推荐的forkJoin方法

let observables1 = [
  this.datosAdminAcumul$,
  this.escalaAcumul$,
  this.seccionesAcumul$,
  this.rubricaUnoAcumul$
];
let observables2 = [
  this.datosAdminAcumul$,
  this.escalaAcumul$,
  this.seccionesAcumul$,
  this.rubricaDosAcumul$
];

this.optionObs$.subscribe(option => {
  if (option == "one") {
    forkJoin(...observables1).subscribe(x => {
      (data1 = x[0]),
        (data2 = x[1]),
        (data3 = x[2]),
        (data4 = x[3]);
        // i've tried `console.log()` here but it returns me four undefined values
    });
       // i've tried `console.log()` here, same result.
  } else {
    forkJoin(...observables2).subscribe(x => {
      let pautaFinal = {
        admin: x[0],
        escala: x[1],
        rubrica: x[2],
        secciones: x[3]
      };
    });

1 个答案:

答案 0 :(得分:1)

您可以将forkJoin用于可观察对象的数组

let observables = [obs1, obs2];

forkJoin(...observables).subscribe(x => {
  let result1 = x[0];
  let result2 = x[1];
});

编辑:*首先,我需要将其消耗掉,以进行forwardObsOne或forwardObsTwo *

let resultObservable =  return this.observable1().pipe(mergeMap((param1) => {
  let observables = [obs1];
  if (param1.condition == true)
     observables.push(obs2);

  return forkJoin(...observables).pipe(map((param1) => {

    ....        

    return <result>;
  }));
}));

resultObservable.subscribe(x => {
   ...
});