gpt4 book ai didi

javascript - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked using Angular2

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:05 24 4
gpt4 key购买 nike

我曾尝试解决此错误 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.先前的值:“未定义”。当前值:'0'。这样:

TS:

 import {
ChangeDetectionStrategy
} from '@angular/core';
@Component({
templateUrl: './page.html'
changeDetection: ChangeDetectionStrategy.OnPush
})
public Pages: Object;

ngOnInit() {
loadAllGuides() {
this.ApiService
.getAllPages()
.subscribe(
Pages => {
this.Pages = Pages.data;
}, error => {
console.log(error);
});
}
}

当我使用 ChangeDetectionStrategy 时,错误在控制台中得到解决,但我还面临另一个问题,除非我在页面打开时点击一下,否则页面不会显示任何数据。一旦我随机点击页面,数据就会加载。谁能帮帮我。

最佳答案

首先删除changeDetection: ChangeDetectionStrategy.OnPush

其次,你有:
public Pages:Object; => 避免对变量使用第一个大写字母,因此请改用 public pages:Object;

那么你有:

 Pages => {
this.Pages = Pages.data;
}

尝试:

response => {
this.pages = response.data;
}

关于javascript - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked using Angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com