Home > Back-end >  RxJs observe several observables
RxJs observe several observables

Time:09-10

In an older version of RxJs I used the following approach:

combineLatest([
  this.state.select(selector1),
  this.stage.select(selector2)
])
  .pipe(...)
  .subscribe(values => {
    const parameter: any = values[0];
    const data: any = values[1];
   );

In my new project I'm using RxJs 7.5 and this is not possible anymore.

combineLatestWith([
  this.userData.controls["name"].valueChanges,
  this.userData.controls["lastName"].valueChanges,
  this.userData.controls["age"].valueChanges,
  this.userData.controls["birthDate"].valueChanges])
.subscribe((result:any) => {

);

The compiler says: "Property 'subscribe' does not exist on type 'OperatorFunction<unknown, [unknown, any]>'."

The following snipped looks kind of ugly:

this.userData.controls["name"].valueChanges.combineLatestWith([
  this.userData.controls["lastName"].valueChanges,
  this.userData.controls["age"].valueChanges,
  this.userData.controls["birthDate"].valueChanges])
  .subscribe((result: any) => {
    console.log(result);
  });

Is there another solution, similar to the first approach?

CodePudding user response:

You're using wrong combineLatest() import. Since RxJS 7.0 the creation operator is called combineLatest() and is imported from 'rxjs' (this is the one you want to use).

import { combineLatest } from 'rxjs';

...

combineLatest([firstTimer, secondTimer]).subscribe(...);

combineLatestWith() is a pipable operator meant to be used inside RxJS chains (thus the error).

import { combineLatestWith } from 'rxjs';

...

input1Changes$.pipe(
  combineLatestWith(input2Changes$),
  map(([e1, e2]) => ...)
)

  • Related