从子组件向父组件发出输出时获取 ExpressionChangedAfterItHasBeenCheckedError

问题描述

我正在尝试从确认组件中对 getorderConfirmationAction 的调用返回计数,并将其传递给操作弹出组件。它正在工作,但我在控制台中看到的错误是“ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。以前的值:'0'。当前值:'2'。

当我用谷歌搜索错误时,有人建议将输出事件放在 ngAfterViewInit 和 Angular 的其他生命周期中。但这没有帮助。

this.totalCount.emit(this.orderConfirmationResponseList.length.toString());

另外,在 action-popup 组件中,我真的不需要向 onTotalCount 传递任何东西。但我没有看到其他方法

onTotalCount($event)

下面是父(action-popup)组件和子(confirm)组件的简化代码

action-popup.component.html

        <app-confirm (totalCount)="onTotalCount($event)"></app-confirm>
        <div>
          {{totalCount}}
        </div>

action-popup.component.ts

        public totalConfirmCount: number = 0;
        
        onTotalCount(totalCount: number {
            this.totalConfirmCount = totalCount;
        }

confirm.component.ts

        orderConfirmationResponseList: OrderConfirmationResponse[] = [];
        @Output() totalCount = new EventEmitter();
        
        
        ngOnChanges(): void {
            this.api.getorderConfirmationAction()
                .subscribe((res: OrderConfirmationResponse[]) => {
                    this.orderConfirmationResponseList = res["data"];
            }
            
            this.totalCount.emit(this.orderConfirmationResponseList.length.toString());
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)