gpt4 book ai didi

typescript - 如何使用@viewChild 在我的组件中获取 bradmartin/nativescript-videoplayer 元素引用?

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:38 24 4
gpt4 key购买 nike

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>

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

关于typescript - 如何使用@viewChild 在我的组件中获取 bradmartin/nativescript-videoplayer 元素引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44803299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com