我正在尝试将YouTube嵌入API插入到我的angular 2项目中,该项目是用angular cli构建的。
当我尝试这个问题时,我得到了一个“找不到名字'YT'”的提示: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",但是我不知道如何使用angular来解决这个问题。
发布于 2018-06-03 03:15:04
将它添加到我的导入中,用于引用YT的文件似乎是可行的。
import 'youtube';
有人能解释一下为什么吗?我知道这与import * from 'moment';
很相似,但我也从未真正理解过这一点。
https://stackoverflow.com/questions/45811033
复制相似问题