我在我的 Angular 项目中使用 videojs 插件。我试图访问 videojs 方法中的值和方法,但在组件初始化时它显示未定义的值。我也尝试在 ngAfterViewInit 中调用 videojs 方法,但它仍然没有显示 videojs 方法中组件的值。如何在 videojs 方法中显示变量值?谁能帮我?
组件代码:
import {Component,OnInit} from '@angular/core';
declare var videojs :any;
@Component({
selector: 'app-play-video',
templateUrl: './play-video.component.html',
styleUrls : []
})
export class PlayVideoComponent implements OnInit{
public videoJSplayer :any;
public videoUrl :string;
constructor(){
this.videoUrl = 'http://clips.vorwaerts-gmbh.de/VfE_html5.mp4';
}
showValues(){
console.log("show Values method called");
}
ngOnInit() {
this.videoJSplayer = videojs(document.getElementById('play_video_id'), {}, function() {
console.log(this.videoUrl); // here the video url value is undefined
this.showValues(); // this also undefined
this.play();
}
}
}
播放视频.component.html:
<video id="play_video_id" class="video-js vjs-default-skin vjs-big-play-centered"
controls preload="auto" width="640" height="264"
poster="http://video-js.zencoder.com/oceans-clip.png"
data-setup='{"example_option":true}'>
<source [src] = videoUrl type="video/mp4" />
</video>
最佳答案
你必须使用ES6 arrow functions以便回调在回调中获取正确的 this
上下文。当您使用 function() {}
语法时,内部的 this
将根据调用上下文而变化:
this.videoJSplayer = videojs(document.getElementById('play_video_id'), {}, () => {
// `this` will point to the current `PlayVideoComponent` instance
}
)
关于javascript - 如何访问Angular 2中VideoJs函数中的成员变量和方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254405/