我的index.html页面上有几个自定义的角元素,名为<my-video>,每个元素都通过YouTube Iframe加载视频播放器。我为onYouTubeIframeAPIReady回调定义了一个回调,该回调是在加载了API脚本时在window对象上触发的。回调设置视频播放器。为了设置多个播放器,我在回调的内部手动调用窗口‘’onYouTubeIframeAPIReady‘(它自己调用自己)。结果是:每个球员的负载都很好,功能也和预期的一样。在最后一个播放器创建后,我得到一个错误:"Uncaught : youTubeIframeAPIReady不是一个函数“。对于我的生活,我不知道如何解决控制台TypeError。请帮帮忙。
这是组件库中的视频组件,使用的是角自定义元素,而不是完整的应用程序。
Index.html
<my-video some-property="some value"></my-video>
<my-video some-property="some value"></my-video>Video.component.ts
import { Component, OnInit} from '@angular/core';
import { Observable } from 'rxjs';
@Component({
  selector: 'my-video',
  templateUrl: './video.component.html',
  styleUrls: ['./video.component.scss']
})
export class VideoComponent implements OnInit {
  videoYoutubeId = 'WWXfdIK1WxA';
  private player: any;
  private YT: any;
  constructor() {}
  ngOnInit() {
    this.loadYoutubeScript();
    this.createVideoPlayer();
  }
  loadYoutubeScript() {
    const tag = document.createElement('script');
    tag.src = 'https://www.youtube.com/iframe_api';
    const firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  }
  createVideoPlayer() {
    const youTubeIframeAPIReady = window['onYouTubeIframeAPIReady'];
    window['onYouTubeIframeAPIReady'] = () => {
      this.YT = window['YT'];
      const iframeId = this.videoYoutubeId.toLowerCase();
      this.player = new window['YT'].Player(iframeId, {
        events: {
          'onReady': this.onPlayerReady.bind(this)
        }
      });
      youTubeIframeAPIReady(); // <==== Uncaught TypeError: youTubeIframeAPIReady is not a function
    };
  }
}Video.component.html
<iframe
   [id]="videoYoutubeId | lowercase"
   width="640"
   height="360"
   style="width: 100%; height: 100%; padding: 0; margin: 0;"
   frameborder="0"
   src="https://www.youtube.com/embed/WWXfdIK1WxA?controls=1&rel=0&showinfo=0&autoplay=0&modestbranding=1&enablejsapi=1&html5=1&fs=0">
</iframe>我希望能够加载几个视频元素到我的索引页面和每一个视频有自己的播放器,然后我可以检索信息或控制通过类型记录。我需要一种方法来消除错误:"Uncaught : youTubeIframeAPIReady不是一个函数“,在最后一个播放器创建之后。
发布于 2019-07-10 05:28:19
从匿名函数调用另一个方法的最佳方法是使用箭头函数来触发以下方法:
设readyFuction = () => this.youTubeIframeAPIReady();
https://stackoverflow.com/questions/56963304
复制相似问题