如何指定“异步方式”变量的类型?

时间:2019-05-30 08:16:12

标签: angular observable

我从此标记中收到错误Argument of type '() => Observable<Device[]>' is not assignable to parameter of type 'Promise<any>'

<div *ngIf="(deviceStoreService.getDevices | async) as devices">
  <tr *ngFor="let device of devices">
    <td>{{device.name}} <midi-keyboard [device]="device"></midi-keyboard></td>
  </tr>
</div>

我从这个标记中得到了相同的错误:

<tr *ngFor="let device of (deviceStoreService.getDevices | async)">
  <td>{{device.name}} <midi-keyboard [device]="device"></midi-keyboard></td>
</tr>

如果我分配带有标记的控制器类成员变量,我不会收到错误消息:

<tr *ngFor="let device of (devices | async)">
  <td>{{device.name}} <midi-keyboard [device]="device"></midi-keyboard></td>
</tr>

和控制器类:

export class DeviceComponent implements OnInit {

  devices: Observable<Array<Device>>;

  constructor(
    private deviceStoreService: DeviceStoreService,
  ) { }

  ngOnInit() {
    this.devices = this.deviceStoreService.getDevices();
  }

这是为什么?

1 个答案:

答案 0 :(得分:0)

您缺少父母。如果getDevices是一个函数,则

<div *ngIf="(deviceStoreService.getDevices() | async) as devices">