【发布时间】:2019-11-04 14:37:28
【问题描述】:
当视频切换(播放/暂停)时,我正在尝试更改视频的自定义图标。
ngAfterViewInit() {
const vdoCont = document.querySelector('.video-player');
const vdo = vdoCont.querySelector('video');
vdo.addEventListener('play', () => {
console.log(this) // Here "this" refers to typescript class
this.updateVdoIcon(this);
});
vdo.addEventListener('pause', () => {
console.log(this) // Here "this" refers to typescript class
this.updateVdoIcon(this);
});
}
updateVdoIcon(videoElment: any) {
console.log(videoElment); // Expecting this to be video element instead of typescript class
}
我尝试将箭头函数更改为 JavaScript 函数,但在这里我无法使用我的“updateVdoIcon”函数。
vdo.addEventListener('play', function() {
this.updateVdoIcon(this); // Property 'updateVdoIcon' does not exist on type 'HTMLVideoElement'
});
我知道我可以使用匿名函数(如下所示)并在此处更新图标,但是如果我有很多代码要在函数中分离,该怎么办
vdo.addEventListener('play', function() {
this.paused ? console.log('Play icon') : console.log('Pause icon')
});
【问题讨论】:
标签: javascript angular typescript this