【问题标题】:rxjs/Subscription undefined value of another subscriptionrxjs/Subscription 另一个订阅的未定义值
【发布时间】:2017-08-20 13:51:44
【问题描述】:

我需要将另一个订阅的值用于订阅。这是代码。产品价值未定义,获取价值的最佳方法是什么?

代码:

@Component({
    selector: 'product-page',
    templateUrl: 'product-page.html'
})
export class ProductPage {
    private product: Product;
    this.store.select('products').subscribe(({
        product
    }) => {
        if (product) {
            this.product = product;
        }
    });
    this.store.select('plc').subscribe((value: any) = > {
        console.log(this.product); // undefined
    })
}

【问题讨论】:

    标签: rxjs


    【解决方案1】:

    您可以使用combineLatest operator 组合两个选项:

    import 'rxjs/add/operator/combineLatest';
    // ...
    
    this.store
      .select('products')
      .combineLatest(this.store.select('plc'))
      .subscribe(([products, plc]) => {
        // ...
      });
    

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多