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
相关产品推荐
相关产品推荐

