Angular2 上的 onYouTubeIframeAPIReady 找不到名称“YT" [英] onYouTubeIframeAPIReady on Angular2 can't find name 'YT'

查看:31
本文介绍了Angular2 上的 onYouTubeIframeAPIReady 找不到名称“YT"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 angular 2 项目中插入 YouTube 嵌入 API,使用 angular cli 构建.

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

当我尝试在这个问题上评论的方法时,我得到一个找不到名字‘YT’":onYouTubeIframeAPIReady 未在 angular2 网络应用程序上触发

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);
};

我不确定应该在哪里以及如何获取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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆