问题描述
我正在尝试在我使用angular cli构建的angular 2项目中插入YouTube embed API.
I'm trying to insert the YouTube embed API on my angular 2 project, built with angular cli.
当我尝试对此问题评论的方法时,我得到找不到名称'YT'":
I get a "can't find name 'YT'" when I try the approach commented on this question: onYouTubeIframeAPIReady not firing on angular2 web app
我的服务代码如下:
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);
};
我不确定应该在哪里以及如何获得"YT"结构.我知道它来自" https://www.youtube.com/iframe_api ",但是我不知道该如何解决这个问题.
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.
推荐答案
将其添加到我的导入中以引用YT的文件似乎可行.
Adding this to my imports for the file referencing YT seems to work.
import 'youtube';
有人可以解释原因!我意识到这与import * from 'moment';
类似,但我也从未真正理解过.
Can someone please explain why! I realize this is similar to import * from 'moment';
but I never really understood that either.
这篇关于Angular2上的onYouTubeIframeAPIReady找不到名称"YT"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!