首页 > 解决方案 > Angular 6 - 参数类型订阅不可分配给参数类型数组

问题描述

试图在表格上显示数据。

智能表组件.ts

export class SmartTableComponent {
clients: Client[];
totalOwed: number;

source: LocalDataSource = new LocalDataSource();

constructor(private clientService: ClientService) {
  const data = this.clientService.getClients().subscribe(clients => this.clients = clients);
  this.source.load(data); // this line returns the error below
}

这是错误:

参数类型订阅不可分配给参数类型数组

如果我继续我的 ClientService,这就是 getClients() 的作用:

getClients(): Observable<Client[]> {
this.clients = this.clientsCollection.snapshotChanges()
  .map(changes =>  {
    return changes.map(action => {
      const data = action.payload.doc.data() as Client;
      data.id = action.payload.doc.id;
      return data;
    });
  });
return this.clients;
}

那么,如何将订阅转换为数组?

标签: angularfirebasegoogle-cloud-firestore

解决方案


而不是分配给数据直接将客户端绑定到源,如下所示,

this.clientService.getClients().subscribe(clients =>{ 
   this.clients = clients;
   this.source.load(clients);
});

推荐阅读