在change typescript上观察firebase数据库

问题描述 投票:0回答:1

所以我在角色服务中调用我的firebase实时数据库,如下所示:

readItems() {
    return this.af.database.ref(`/path`)
      .on('value', snap => this.callback(snap.val()));
  }

回调只是修改了对我需求的响应。我需要做的是在我正在调用readItems的组件中获取此修改后的数据。这是在所述组件中的调用:

this.service.readItems();

我希望在完成时找到并使用带有subscribethen的observable或promise返回修改后的响应。我无法想象如何做到这一点,任何帮助都会很棒。

angular typescript firebase firebase-realtime-database angular5
1个回答
2
投票

您可以在传递给on函数的回调中使用来自rxjs的Subject并订阅它:

valueChanged$ = new Subject();

callback(val) {
    // some code ...
    valueChanged$.next(val); // pass whatever you need to receive in the subscription
}

readItems() {
  return this.af.database.ref(`/path`)
    .on('value', snap => this.callback(snap.val()));
}

然后在你的代码中:

this.valueChanged$.subscribe(data => doStuff(data));
this.service.readItems();
© www.soinside.com 2019 - 2024. All rights reserved.