Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何处理具有严重可观测属性的可观测对象?_Angular_Rxjs - Fatal编程技术网

Angular 如何处理具有严重可观测属性的可观测对象?

Angular 如何处理具有严重可观测属性的可观测对象?,angular,rxjs,Angular,Rxjs,我能观察到一个物体。当我订阅时,其他属性(自我定义为可观察)已添加到对象中 const obj = { a: 'some text' }; const b$ = of('BBB').pipe(delay(1000)); const c$ = of('CCC'); const obj$ = of(obj).pipe(delay(100)); const res$ = obj$.pipe( map(obj => Object.assign({ b: b$, c: c

我能观察到一个物体。当我订阅时,其他属性(自我定义为可观察)已添加到对象中

const obj = {
  a: 'some text'
};
const b$ = of('BBB').pipe(delay(1000));
const c$ = of('CCC');

const obj$ = of(obj).pipe(delay(100));

const res$ = obj$.pipe(
  map(obj => Object.assign({
    b: b$,
    c: c$
  }, obj))
)

const subscribe = res$.subscribe(val => console.log(val));
输出仍然包含可观测值:

{a: "some text", b: Observable, c: Observable}
但预期产出应为:

{a: "some text", b: "BBB", c: "CCC"}
我怎样才能解决这个问题?下面是StackBlitz:

CombineTest会帮你达到目的

combineLatest(b$, c$, obj$)
  .pipe(
    map(([b, c, obj]: any) => ({
      b,
      c, 
      obj
    })),
  )
  .subscribe(console.log);
任何时候它们中的任何一个发出一个值,您的最终订阅就会触发,例如,执行以下操作:

const c$ = interval(1000).pipe(map( () => of('CCC')));
每秒钟都会重新发射一次。或者,interval1000会每秒更改C的值。

CombineTest会让您达到这一点

combineLatest(b$, c$, obj$)
  .pipe(
    map(([b, c, obj]: any) => ({
      b,
      c, 
      obj
    })),
  )
  .subscribe(console.log);
任何时候它们中的任何一个发出一个值,您的最终订阅就会触发,例如,执行以下操作:

const c$ = interval(1000).pipe(map( () => of('CCC')));

每秒钟都会重新发射一次。或者interval1000每秒都会更改C的值。

如果您想等到所有的观察都完成,那么您可能需要尝试forkJoin


这将在触发订阅块之前,等待这些可观察对象中的每一个都完成。

如果要等待所有可观察对象完成,则可能需要尝试forkJoin


在触发订阅块之前,这将等待所有这些可观察对象完成。

您查看了CombineTest?您查看了CombineTest?非常感谢,CombineTest完成了。非常感谢,CombineTest完成了。