我正在尝试将@Output与事件发射器一起使用,以在子组件与父组件之间进行通信以传递变量。我遵循了本教程:https://dzone.com/articles/understanding-output-and-eventemitter-in-angular
我研究了与此相关的不同stackoverflow问题,但没有一个适合我的情况。
子HTML
<button (click)="onLinkedClicked();">Click me</button>
儿童TS
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
.
.
.
export class showcaseMenuComponent implements OnInit {
@Output() public linkedClicked = new EventEmitter<String>();
constructor() {}
onLinkedClicked() {
console.log('on click child');
this.linkedClicked.emit('collapsed');
}
}
它将打印日志“点击子项”
父HTML
<showcase-menu #topNavMenu [showBranding]="false"
[showSearch]= "false" (onLinkedClicked)="linkToggler($event)"></showcase-menu>
父级TS
.
.
.
navMenuState: string;
.
.
.
linkToggler($event) {
console.log("partent");
this.navMenuState = $event;
}
linkToggler()永远不会被调用。而且我在控制台中没有任何错误,
答案 0 :(得分:4)
该事件必须与与Output装饰器关联的属性的名称匹配
<showcase-menu #topNavMenu
[showBranding]="false"
[showSearch]= "false"
(linkedClicked)="linkToggler($event)">.
</showcase-menu>