尝试在表格上显示数据。
Smart-table-component.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
}
这是错误:
参数类型订阅不能分配给参数类型Array
如果我继续使用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;
}
那么,如何将订阅转换为数组?
答案 0 :(得分:0)
按如下所示,而不是直接将客户端绑定到源,而不是分配数据:
this.clientService.getClients().subscribe(clients =>{
this.clients = clients;
this.source.load(clients);
});