【问题标题】:Filling the screen with data from several observables用来自多个可观察对象的数据填充屏幕
【发布时间】:2019-10-02 18:28:49
【问题描述】:

我有一个由 firestore 返回的对象填充的屏幕,但我无法在同一个 onInit 上返回两个查询的数据

服务:

      export class TcpService implements MPersist{
      tcpsO: Observable<TcpID[]>;
      private tcpCollection: AngularFirestoreCollection<Tcp>;
      constructor(private db: AngularFirestore) { }

      addDoc(data: Tcp) {
        this.tcpCollection.add({ nome: data.nome, nivel: data.nivel, registro: data.registro })
      }
      editDoc(data:TcpID){
        this.tcpCollection.doc(data.id).set({ nome: data.nome, nivel: data.nivel, registro: data.registro })
      }

      removeDoc(id){
        this.tcpCollection.doc(id).delete();
      }

      getDocs() {
        this.tcpCollection = this.db.collection<Tcp>('tcps');
        return this.tcpsO = this.tcpCollection.snapshotChanges().pipe(
          map(actions => actions.map(a => {
            const data = a.payload.doc.data() as Tcp;
            const id = a.payload.doc.id;
            return { id, ...data }
          }))
        );

      }
    }
    export interface TcpID extends Tcp { id: string }

对于组件:

    ngOnInit() {
        return this.service.getDocs().subscribe(
          res => {
            this.dataSource.sort = this.sort;
            this.dataSource.data = res;
          }
        );
      }

所以我可以操作数据,但是当我尝试返回 2 个不同的查询不起作用时,我需要用来自 2 个不同查询的信息填充组件

【问题讨论】:

  • 您可以使用 combineLatest 或 forkJoin 可观察运算符,具体取决于您的用例

标签: angular typescript google-cloud-firestore observable


【解决方案1】:

当使用 DataSources 时,建议在更改数据时设置一个新的 DataSource。所以基本上是这样的:

this.dataSource = new DataSource(res)

可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2017-08-17
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多