Javascript 无法使用音频源angular 8读取angular中的属性“nativeElement”

Javascript 无法使用音频源angular 8读取angular中的属性“nativeElement”,javascript,angular,events,Javascript,Angular,Events,我不知道为什么,但在我的情况下,每次我都会犯这样的错误: ERROR TypeError: Cannot read property 'nativeElement' of undefined 我知道在angular 8中,我必须在我的viewChild中指定它是动态的。 这是我的代码: // my var @ViewChild('currentMusic', { static: false }) audioRef: ElementRef; // my listener th

我不知道为什么,但在我的情况下,每次我都会犯这样的错误:

ERROR TypeError: Cannot read property 'nativeElement' of undefined
我知道在angular 8中,我必须在我的viewChild中指定它是动态的。 这是我的代码:

  // my var
  @ViewChild('currentMusic', { static: false }) audioRef: ElementRef;


  // my listener
  this.audioRef.nativeElement.addEventListener('timeupdate', (data: any) => {
    console.log('timer :' + data.target.currentTime)
  })

  // and in my HTML
  <div hidden *ngIf="(audio$ | async); let m">
    <audio #currentMusic autoplay>
      <source [src]="baseUrl + m.file.url" type="audio/mpeg">
    </audio>
  </div>

因此,当我显示页面时,我的音频$| async为空,我必须选择一个链接来播放音乐。我只能用一个球员。如果您有解决方案,谢谢您

我建议不要使用async inside*ngIf,因为它无法与ViewChild正常工作:

解决方案: 使用async直接从音频元素捕获事件timeupdate: HTML:

PS:在不使用| async的情况下对我有效

  <audio #currentMusic (timeupdate)="onTimeUpdate($event)" autoplay>
     <source [src]="baseUrl + m.file.url" type="audio/mpeg">
  </audio>
  onTimeUpdate(event: any) {
    console.log('timer :' , event.target.currentTime)
  }