Javascript 如何为angular2导入视频js?
我尝试用视频js播放器构建angular2应用程序,但我不知道如何调用。我尝试example.component.html,就像没有工作一样,然后尝试example.component.ts,就像Javascript 如何为angular2导入视频js?,javascript,html,angular,video.js,Javascript,Html,Angular,Video.js,我尝试用视频js播放器构建angular2应用程序,但我不知道如何调用。我尝试example.component.html,就像没有工作一样,然后尝试example.component.ts,就像 var videojs=require('video.js'); var player = videojs('my-player'); Angular 2+是用TypeScript编写的,因此您需要VideoJS的TypeScript定义才能在Angular 2中正确使用库的API。为此,请通过n
var videojs=require('video.js');
var player = videojs('my-player');
Angular 2+是用TypeScript编写的,因此您需要VideoJS的TypeScript定义才能在Angular 2中正确使用库的API。为此,请通过npm安装@types/videojs:
npm安装@types/videojs
现在,您可以使用import
语句导入VideoJS。
另外,请注意,在TypeScript中仍然可以编写纯JS
无论如何,您试图实现的可能不起作用,因为组件的模板和逻辑在捆绑之前是分开的,不像VideoJS示例中的脚本位于HTML模板内部。
因此,您需要使用document.getElementById
传递元素本身
videojs(document.getElementById('my-player'));
我建议您参考。Angular 2+是用TypeScript编写的,因此您需要VideoJS的TypeScript定义,以便能够在Angular 2中正确使用库的API。为此,请通过npm安装@types/videojs:
npm安装@types/videojs
现在,您可以使用import
语句导入VideoJS。
另外,请注意,在TypeScript中仍然可以编写纯JS
无论如何,您试图实现的可能不起作用,因为组件的模板和逻辑在捆绑之前是分开的,不像VideoJS示例中的脚本位于HTML模板内部。
因此,您需要使用document.getElementById
传递元素本身
videojs(document.getElementById('my-player'));
我建议您参考。简单步骤:
1. <代码>npm安装--保存video.js
2. <代码>npm安装--保存dev@types/video.js
安装后,只需像这样导入。现在我们也可以像这样使用接口了
import * as videojs from 'video.js'
class VideoComponent {
public videoJS = videojs.default
private videoPlayer: videojs.VideoJsPlayer
private options: videojs.VideoJsPlayerOptions
someMethod() {
this.videoJS('my-player', {
controls: true,
autoplay: true,
preload: 'auto'
});
}
}
简单步骤:
1. <代码>npm安装--保存video.js
2. <代码>npm安装--保存dev@types/video.js
安装后,只需像这样导入。现在我们也可以像这样使用接口了
import * as videojs from 'video.js'
class VideoComponent {
public videoJS = videojs.default
private videoPlayer: videojs.VideoJsPlayer
private options: videojs.VideoJsPlayerOptions
someMethod() {
this.videoJS('my-player', {
controls: true,
autoplay: true,
preload: 'auto'
});
}
}
你能说得更具体些吗?你不知道怎么打电话是什么意思?你这么说它不起作用是什么意思?请您也添加
示例.component.html
代码好吗?您能更具体一点吗?你不知道怎么打电话是什么意思?你这么说它不起作用是什么意思?您可以添加example.component.html
代码吗?答案很好,但是使用@ViewChild
可能比调用getElementById
更好directly@user184994完全同意。这显然更具角度感。plunkr现在似乎已经坏了。答案很好,但使用@ViewChild
可能比调用getElementById
directly@user184994完全同意。那肯定更像是棱角分明的东西了。这个弹夹现在好像坏了。谢谢-这很有效。我以前从未见过像这样使用packagename.default。我们为什么要这么做?他们只是没有进行默认导出吗?谢谢-这很有效。我以前从未见过像这样使用packagename.default。我们为什么要这么做?他们只是没有进行默认导出吗?