月關寶盒
2023-03-24 15:44:14
我有兩個組成部分。組件 A 是父組件,組件 B 是子組件。組件 A 如下所示:一個.html <nb-box [onCreditChange]="onCreditChange"></nb-box>A.tsonCreditChange($event) { console.log($event) }組件 A 的功能轉移到 B。組件 B 看起來像這樣B.html<div class="box"> <nb-switch (onChange)="onCreditChange($event)"></nb-switch></div>B.ts(該組件的一部分)import { Component, Input, NgModule, EventEmitter, Output} from '@angular/core';export class Box extends BoxBase { @Output() onCreditChange: EventEmitter<any> = new EventEmitter()}調用函數時出現錯誤core.js:6241 ERROR TypeError: ctx.onChange is not a function你知道怎么修嗎?
1 回答

LEATH
TA貢獻1936條經驗 獲得超7個贊
父組件
HTML
<nb-box (onCreditChange)="onCreditChange"></nb-box>
TS
onCreditChange($event) { console.log($event) }
子組件
錯誤從這里開始,因為 Output 不是一個函數,它是一個允許您將事件發送給父級的對象。您需要在子函數內部執行一個函數,并使用輸出對象發出。
HTML
<div class="box">
<nb-switch (onChange)="onChangeInChild($event)"></nb-switch>
</div>
TS
import { Component, Input, NgModule, EventEmitter, Output} from '@angular/core';
export class Box extends BoxBase {
@Output() onCreditChange = new EventEmitter<any>()
onChangeInChild(eventObject: any){
this.onCreditChange.emit(eventObject)
}
}
添加回答
舉報
0/150
提交
取消