You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular中EventEmitter在元素更新时失效的问题求助

Angular中EventEmitter在元素更新时失效的问题求助

我最近在Angular项目里用EventEmitter的时候碰到个头疼的问题——创建元素的时候这个事件发射器工作得好好的,可一旦尝试更新元素,它就完全没反应了,真的搞不懂哪里出问题了😩

我是这么声明EventEmitter的:

@Output() public updatedIngredient = new EventEmitter<boolean>();

创建元素的函数大概是这样的:

public async createIngredient(): Promise<void> {
    try {
        if (this.ingredientForm.valid) {
            await this.ingredientService.createIngredient(this.ingredientForm.value);
            this.updatedIngredient.emit(true);
            this.closeModal();
        }
    } catch (error) {
        console.error('创建食材失败:', error);
    }
}

而更新元素的函数我是这么写的:

public async updateIngredient(): Promise<void> {
    try {
        if (this.ingredientForm.valid) {
            await this.ingredientService.updateIngredient(this.currentIngredient.id, this.ingredientForm.value);
            this.updatedIngredient.emit(true);
            this.closeModal();
        }
    } catch (error) {
        console.error('更新食材失败:', error);
    }
}

我确认过更新的API请求已经成功返回了,可父组件就是接收不到updatedIngredient这个事件。有没有过来人能帮我排查下问题呀?

备注:内容来源于stack exchange,提问作者Gerard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 16:24:39