简体   繁体   English

为什么@Output不发出任何东西(角度8)

[英]Why @Output doesn't emit anything (in angular 8)

I'm trying to transfer an object to a pot component via @Output. 我正在尝试通过@Output将对象传输到锅组件。

But emit doesn't return anything and when you put it in the log it's undefined. 但是emitt不返回任何东西,当您将其放入日志时,它是未定义的。

This is my code: 这是我的代码:

Transmitter component : 发射器组件:

@Output() menubuttonclicked = new EventEmitter<object>();
.
.
.
clickedmenu(id: string) {
    this.rclickemit.buttonid = id ;
    this.menubuttonclicked.emit(this.rclickemit);
  }

Transmitter html: 发射器html:

<button *ngFor ="let button of Setting.menu" (click)="clickedmenu(button.id)" mat-menu-item>
    <mat-icon>{{button.icon}}</mat-icon>
    <span>{{button.name}}</span>
  </button>

Recipient Component: 收件人组成:

ngOnChanges(changes: SimpleChanges): void {
    if (changes['emit']) {
      console.log(this.emit);
    }
  }

But nothing can be printed on the console. 但是,控制台上无法打印任何内容。 I gave the emit variable to get the output but no value in it. 我给了emit变量以获得输出,但其中没有值。 Does anyone know the problem? 有人知道这个问题吗?

Update 更新

Recipient Component: 收件人组成:

<app-ir-graph-d3  [node]="nodes" [link]="links" [Setting]="Setting" (menubuttonclicked)="emit"></app-ir-graph-d3>

Output event emitter is used as an attribute with parentheses. 输出事件发射器用作带括号的属性。 For example: 例如:

In your recipient template: 在您的收件人模板中:

<app-transmitter-component (menubuttonclicked)="onMenuButtonClicked($event)"></app-transmitter-component>

In your recipient component: 在收件人组件中:

onMenuButtonClicked = (buttonID) => {
   console.log(buttonID)
}

For further reading: https://angular.io/guide/component-interaction 进一步阅读: https : //angular.io/guide/component-interaction

ngOnChanges is called when a change happend at data-bound properties such as @Input properties. ngOnChanges当变化在数据绑定性能如happend称为@Input性质。

OnChange: https://angular.io/api/core/OnChanges OnChange: https//angular.io/api/core/OnChanges

You need to handle of Output . 您需要处理Output Let me show an example: 让我举一个例子:

yourOutputComponent.ts: yourOutputComponent.ts:

export class yourOutputComponent implements OnInit {
  @Output() change = new EventEmitter();

  onClick(){
    this.change.emit({newValue: 'I am your data' });
  }
}

and subscribed component fooComponent.html : 并订阅了组件fooComponent.html

<yourOutput (change)="hello($event)"></yourOutput>

fooComponent.ts: fooComponent.ts:

export class fooComponent implements OnInit {
    hello(event:any) {
        console.log(event);
    }
}

You should listen to the changes in the html of the Recipient Component not the ngOnChanges 您应该收听收件人组件的html中的更改,而不是ngOnChanges

<recipient-component (menubuttonclicked)='clickHandler($event)'></recipient-component>

Then in the typescript file, you declare teh clickHandler function and receive the input there. 然后在打字稿文件中,声明clickHandler函数并在此处接收输入。

Wherever in the Transmitting component you have used Recipient Component Please write eventEmmiter variable, and listen that event in the Recipient Component. 无论在“传输”组件中的何处使用了“收件人”组件,请编写eventEmmiter变量,然后在“收件人”组件中监听该事件。

<Recipient Component (menubuttonclicked) = "matButtonClicked($event)">

Recipient Component 收件人组成

matButtonClicked(event) {
 console.log(event) // 'this.rclickemit'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM