Reference to the third party player on github.
问题:
如何使用VIEWHORD在我的组件中获得第三方NATESVScript视频播放器的元素引用?
错误:
当我试图通过这个.VooPoop.Poad()访问方法播放时,我的代码编译但崩溃了。
类型错误:this.videoplayer.play不是函数。(在
“this.videoplayer.play()”,“this.videoplayer.play”
未定义)
代码
播放器.component.ts
import {Component, OnInit, ViewChild} from '@angular/core';
import {registerElement} from "nativescript-angular/element-registry";
registerElement("VideoPlayer", () => require("nativescript-videoplayer").Video);
@Component({
selector: "Player",
moduleId: module.id,
templateUrl: "./player.component.html",
styleUrls: ["./player.component.css"]
})
export class PlayerComponent implements OnInit{
@ViewChild("video_player") videoPlayer: Video;
public src: string = "<YOUR VIDEO URL HERE>"
ngOnInit(): void {
this.videoPlayer.play();
}
}
player.component.html播放器
<VideoPlayer
#video_player
[src]="src"
height="300"></VideoPlayer>
github issue reference #77
最佳答案
在检查this.videoPlayer
的属性之后,通过:
for(let prop in this.videoPlayer){
if(this.videoPlayer.hasOwnProperty(prop)){
console.dir(prop);
}
}
我注意到它只有一个属性“nativeElement”。为了能够使用它,我必须将
viewChild
类型从“Video
”改为“ElementRef
”,这允许我访问nativeElement
,然后允许我访问文档上定义的所有API。import {Component, OnInit, ViewChild, ElementRef} from '@angular/core';
import {registerElement} from "nativescript-angular/element-registry";
registerElement("VideoPlayer", () => require("nativescript-videoplayer").Video);
@Component({
selector: "Player",
moduleId: module.id,
templateUrl: "./player.component.html",
styleUrls: ["./player.component.css"]
})
export class PlayerComponent implements OnInit{
@ViewChild("video_player") videoPlayer: ElementRef;
public src: string = "<YOUR VIDEO URL HERE>"
ngOnInit(): void {
this.videoPlayer.nativeElement.play();
}
}