首页 > 解决方案 > Angular 事件发射问题

问题描述

最近我为朋友启动了一个项目,我需要使用事件。我以前只用过一次(只是提醒一下)。不幸的是,我似乎无法让父母赶上发射。这是一些代码:
发出的孩子:

 @Output() reset: EventEmitter<any> = new EventEmitter();
 changeRoute(val)
 {
    if (val.url = '/' && this.tuneholder.getTune() != null)
    {
         if (this.tuneholder.getTune().type == this.tune.type){
         this.SetTune(this.tuneholder.getTune())
         this.tuneholder.clearTune();
         console.log("Emit");
         console.log(this.tune);
         this.reset.emit(this.tune);
    }
 }

这是父html:

<app-playeritem 
[tune]="tune" 
*ngFor="let tune of tunes"
(reset)='resetTuneVal($event)'>
</app-playeritem>

这是父打字稿:

  resetTuneVal(tune:Tune){
    console.log("Caught Emit");
    console.log(tune);
    this.tunes.forEach(elm => {
       if (elm.type == tune.type){
          this.SetTune(elm, tune);
       }
    });
  }

发出的日志存在,但捕获的日志不存在。有任何想法吗?我觉得我做得很好,但我真的迷路了。

标签: angulartypescriptangular-event-emitter

解决方案


所以我想出了我的问题。我在路由器事件中调用了发出。这意味着父级不是我想象的那样,它实际上是路由器的父级。我通过运行检查ngOnInit()来修复它,因为无论何时加载elem都会自动调用它。


推荐阅读