admin 管理员组文章数量: 1086019
I have two ponents, one videoComponent and videoControlsComponent.
The video ponent contains a <video>
element and the video ponent has some buttons to manipulate the videoComponent <video>
element.
<video controls="{{ controls }}" [src]="streamUrl" #myVideo>
Your browser does not support the video tag or the file format of this video.
</video>
videoComponent:
@ViewChild('myVideo') myVideo: any;
public playVideo() {
this.myVideo.nativeElement.play();
}
videoControlComponent:
constructor(private videoComponent: VideoComponent) { }
public videoPlay() {
this.videoComponent.playVideo()
}
The problem is that when I click the button I get the following error: Cannot read property 'nativeElement' of undefined at VideoControlsComponent
.
But when I have exactly the same code but create the button not in the videoControlsComponent but videoComponent everything works fine.
Can you help me out please?
I have two ponents, one videoComponent and videoControlsComponent.
The video ponent contains a <video>
element and the video ponent has some buttons to manipulate the videoComponent <video>
element.
<video controls="{{ controls }}" [src]="streamUrl" #myVideo>
Your browser does not support the video tag or the file format of this video.
</video>
videoComponent:
@ViewChild('myVideo') myVideo: any;
public playVideo() {
this.myVideo.nativeElement.play();
}
videoControlComponent:
constructor(private videoComponent: VideoComponent) { }
public videoPlay() {
this.videoComponent.playVideo()
}
The problem is that when I click the button I get the following error: Cannot read property 'nativeElement' of undefined at VideoControlsComponent
.
But when I have exactly the same code but create the button not in the videoControlsComponent but videoComponent everything works fine.
Can you help me out please?
Share Improve this question asked Jun 19, 2018 at 19:34 Lil jonsonLil jonson 311 gold badge1 silver badge2 bronze badges1 Answer
Reset to default 6you need to use @ViewChild like you did with "myVideo" with videoComponent as well so like this
@ViewChild(VideoComponent) videoComponent: VideoComponent
that's assuming videoComponent is a child of videoControls
if they are siblings you can use @Output to trigger an event in the parent, the parent would then change a boolean that is set to an input in videoControls and then set up ngOnChanges on videoControls to detect when that input changes
or you can set up a service to municate between them. That might be the easiest option if they are not a parent-child relationship
Example of a Service to municate between ponents:
@Injectable()
export class MyService {
private myFunctionCallSource = new Subject();
myFunctionCalled$ = this.myFunctionCallSource.asObservable();
callMyFunction(){
this.myFunctionCallSource.next()
}
}
in videoComponent
this.myService.myFunctionCalled$.subscribe(
res => this.myVideo.nativeElement.play(),
err => console.log('MyService error', err)
);
in videoControlsComponent
this.myService.callMyFucnction()
本文标签: javascriptAccess viewchild from another componentStack Overflow
版权声明:本文标题:javascript - Access viewchild from another component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743987225a2514074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论