0

Angular 8.3.14 项目。

我们制作了一个 EventEmitter 用于与父组件共享字符串。

子组件

@Output() pepe = new EventEmitter<any>();

ngOnInit() {
   this.pepe.emit('pepe');
}

父组件 HTML

<child-selector (pepe)="sample($event)"></child-selector>

父组件 TS

sample(value) {
    console.log(value);
}

问题是我们在示例函数中接收到一个 CustomEvent 对象。该对象有一个具有当前值的属性,但为什么我们收到的是 CustomEvent 而不是字符串?这是我们从未见过的。

示例函数中收到的 CustomEvent 对象

CustomEvent {isTrusted: false, detail: "Test string", type: "addonsChange", target: wc-ocs-addons-mosaic, currentTarget: wc-ocs-addons-mosaic, …}
isTrusted: false
detail: "Test string"
type: "addonsChange"
target: wc-ocs-addons-mosaic
currentTarget: null
eventPhase: 0
bubbles: false
cancelable: false
defaultPrevented: false
composed: false
timeStamp: 8043.915000045672
srcElement: wc-ocs-addons-mosaic
returnValue: true
cancelBubble: false
path: (8) [wc-ocs-addons-mosaic, div.amena-cms, ocs-theme, app-demo, body, html, document, Window]
__proto__: CustomEvent

自定义事件 IMG

4

1 回答 1

0

您可以在 ngOnInit 之外调用 emit 函数并检查结果。

于 2020-03-15T22:29:15.353 回答