如何使用@viewChild 在我的组件中获取 bradmartin/nativescript-videoplayer 元素引用?
How to get bradmartin/nativescript-videoplayer element reference in my component using @viewChild?
Reference to the third party player on github.
问题:
如何使用 viewChild 在我的组件中获取第三方 nativescript-videoplayer 的元素引用?
错误:
我的代码可以编译,但是当我尝试通过 this.videoPlayer.play()
访问播放方法时崩溃
TypeError: this.videoPlayer.play is not a function. (In
'this.videoPlayer.play()', 'this.videoPlayer.play'
is undefined)
代码:
player.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>
通过以下方式检查 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();
}
}
Reference to the third party player on github.
问题:
如何使用 viewChild 在我的组件中获取第三方 nativescript-videoplayer 的元素引用?
错误:
我的代码可以编译,但是当我尝试通过 this.videoPlayer.play()
访问播放方法时崩溃TypeError: this.videoPlayer.play is not a function. (In 'this.videoPlayer.play()', 'this.videoPlayer.play' is undefined)
代码:
player.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>
通过以下方式检查 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();
}
}