【问题标题】:Angular 6 - Argument type Subscription is not assignable to parameter type Array<any>Angular 6 - 参数类型订阅不可分配给参数类型 Array<any>
【发布时间】:2018-07-15 20:42:59
【问题描述】:

尝试在表格上显示数据。

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
}

这是错误:

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

如果我继续使用我的 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;
}

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

【问题讨论】:

  • 您需要从订阅函数内部调用this.source.load

标签: angular firebase google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

  • 您的答案有效,只是缺少clients =&gt; 之后的花括号
  • 谢谢@Rosenberg,如果反对者有理由发表评论,这是公平的
猜你喜欢
  • 2018-11-25
  • 2021-12-05
  • 2020-03-19
  • 2020-09-08
  • 2021-08-24
  • 1970-01-01
  • 2019-09-06
  • 2018-03-19
  • 2019-10-26
相关资源
最近更新 更多