onYouTubeIframeAPIReady on Angular2 can't find name 'YT'

安稳与你 提交于 2020-01-11 09:42:22

问题


I'm trying to insert the YouTube embed API on my angular 2 project, built with angular cli.

I get a "can't find name 'YT'" when I try the approach commented on this question: onYouTubeIframeAPIReady not firing on angular2 web app

My service code looks like this:

loadAPI() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

(window as any).onYouTubeIframeAPIReady = function () {
  this.player = new YT.Player('player', {
    events: {
      'onReady': this.onPlayerReady,
      'onStateChange': this.onPlayerStateChange
    }
  });
};

(window as any).onPlayerReady = function (event) {
  event.target.playVideo();
};

(window as any).onPlayerStateChange = function (status) {
  console.log(status.data);
};

I'm not sure where and how should I get the 'YT'structure. I know it comes from the "https://www.youtube.com/iframe_api", but I have no idea of how to get angular to work this out.


回答1:


Adding this to my imports for the file referencing YT seems to work.

import 'youtube';

Can someone please explain why! I realize this is similar to import * from 'moment'; but I never really understood that either.



来源:https://stackoverflow.com/questions/45811033/onyoutubeiframeapiready-on-angular2-cant-find-name-yt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!