`Error: Output is not defined` when passing value outside a directive to parent directive

帅比萌擦擦* 提交于 2019-12-02 09:29:51

"@output is giving an error Error: Output is not defined What definition is needed or what is the definition method. I am missing something." :

import {Output} from "angular2/core";

You have to import the definition of any class you are using.

If you want to pass the value to the parent component you can leverage custom event. This code is located in the template of the parent component:

@Component({
  (...)
  template: `
    <dev-table (complete)="someMethod($event.value)"></dev-table>
  `
})
export class ParentComponent {
  someMethod(value) {
    console.log('complete event - value = ' + value);
  }
}

To trigger the event, you could do something like that in your child component:

@Component({
  (...)
  template: `
    (...)
    <span (click)="triggerCompleteEvent()">Trigger complete event</span>
  `
})
export class DevTableComponent implements OnInit{
  @Output()
  complete:EventEmitter;

  constructor() {
    this.complete = new EventEmitter();
  }

  triggerCompleteEvent() {
    this.complete.emit(someValue);
  }
}

someValue corresponds to the value you want the event contains and event subscribes can get.

Hope it helps you, Thierry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!