angular – throws ERROR TypeError:无法读取未定义的属性’emi
发布时间:2020-12-17 06:56:08 所属栏目:安全 来源:网络整理
导读:在MyComponent中,我试图从事件处理程序发出另一个事件. (父组件将使用此新事件执行一些操作).我创建了一个事件发射器作为MyComponent的成员,但事件处理程序方法无法访问事件发射器.它抛出ERROR TypeError:无法读取未定义的属性’emit’.我在StackOverflow上
在MyComponent中,我试图从事件处理程序发出另一个事件. (父组件将使用此新事件执行一些操作).我创建了一个事件发射器作为MyComponent的成员,但事件处理程序方法无法访问事件发射器.它抛出ERROR TypeError:无法读取未定义的属性’emit’.我在StackOverflow上发现了一些相关的问题,但由于对Angular2不熟悉而无法理解.
import { Component,Input,Output,OnChanges,SimpleChanges,OnInit,EventEmitter } from '@angular/core'; import YouTubePlayer from 'youtube-player'; @Component({ selector: 'app-my-component',templateUrl: './my-component.component.html',styleUrls: ['./my-component.component.css'] }) export class MyComponent implements OnChanges,OnInit { @Input() videoURL = ''; player : any; videoId : any; @Output() myEmitter: EventEmitter<number> = new EventEmitter(); ngOnInit(): void { this.player = YouTubePlayer('video-player',{ videoId: this.videoId,width: "100%" }); this.registerEvents(); } private registerEvents() { this.player.on("stateChange",this.onStateChangeEvent); } private onStateChangeEvent(event: any) { console.log("reached here: " + event); this.myEmitter.emit(1); //throws `ERROR TypeError: Cannot read property 'emit' of undefined` } } 有人可以帮帮我吗?请注意,我必须仅从onStateChangeEvent发出事件,因为稍后我会针对不同类型的事件使用不同类型的事件发射器.所以我将把一个switch-case放在onStateChangeEvent中,并使用不同的发射器 – 每种类型一个. 解决方法
通常由错误引起的 private onStateChangeEvent = (event: any) => { console.log("reached here: " + event); this.myEmitter.emit(1); // now correct this } 更多 https://basarat.gitbooks.io/typescript/docs/arrow-functions.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |