簡體   English   中英

ExpressionChangedAfterItHasBeenCheckedError:使用Angular2檢查表達式后,表達式已更改

[英]ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2

我曾嘗試解決此錯誤ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'. ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '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;
}

我認為您需要提供初始價值。

例如 -

public Pages = null;

有關更多信息,請閱讀本文- 您需要了解有關ExpressionChangedAfterItHasBeenCheckedError錯誤的所有信息 它詳細解釋了此錯誤。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM