【问题标题】:How to wait for data from various observables to perform an action如何等待来自各种可观察对象的数据执行操作
【发布时间】:2020-11-21 15:54:56
【问题描述】:

我的角度有问题。我想从各种可观察对象中获取数据,并在所有数据到达后采取行动。

我已经分别尝试了获取地址和数据Person的方法,如果它们运行良好,我将示例留给获取地址的情况。

getDireccion() {
    this.store
        .select('direccion')
        .subscribe(({ direccion }) => (this.direccion = direccion))
        .unsubscribe();
}

但是现在我想确保所有地址数据和人员数据都已经存在以便能够执行另一个操作,这就是我使用 forkJoin 的原因,但它对我来说效果不佳,它没有'不执行console.log ('responses ==>', response);

forkJoin([ 
    this.store.select('direccion'),
    this.store.select('datosPersona')
]).subscribe((response) => {
    console.log('respuestas ==> ', response);
});

我正在使用 ngrx 进行状态处理

【问题讨论】:

  • Esta pagina es para ingles, si puede cambiar la idioma nos podemos ayudarte
  • 用英文试试
  • SO 是一个仅限英文的网站 - 请遵守网站规则,将您的问题(包括标题!)翻译成英文,或者发布在Spanish Stackoverflow site

标签: angular redux rxjs ngrx


【解决方案1】:

您应该使用combineLatest,如果您想在组件生命周期中发生更改时执行某些操作,或者使用take(1) 仅选择一次。它不起作用的原因是因为 forkJoin 等待 observable 完成并且存储 select 从未真正完成:

combineLatest([ 
    this.store.select('direccion'),
    this.store.select('datosPersona')
]).subscribe((response) => {
    console.log('respuestas ==> ', response);
});

forkJoin([ 
    this.store.select('direccion').pipe(take(1)),
    this.store.select('datosPersona').pipe(take(1))
]).subscribe((response) => {
    console.log('respuestas ==> ', response);
});

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 2019-02-25
    • 2022-08-19
    • 2019-03-02
    • 2016-01-21
    • 2016-01-17
    • 1970-01-01
    • 2017-11-15
    • 2019-12-05
    相关资源
    最近更新 更多