简体   繁体   English

离子如何修复“ExpressionChangedAfterItHasBeenCheckedError”错误?

[英]Ionic how to fix “ExpressionChangedAfterItHasBeenCheckedError” error?

//html //html

    <span style="margin-left:43%;background-color:rgb(229,229,229);border-
    radius:10%">&nbsp;&nbsp;{{formatEpoch(epoch)}}&nbsp;&nbsp;</span>

//ts //ts

lastdate:any;                    

 formatEpoch(epoch): string {
    if(epoch == this.lastdate){
        return '';
    }else{ 
    this.lastdate =epoch;
    return UtilService.getCalendarDay(epoch);
    }
  }

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 Previous value: '今天 5:34 PM'.以前的值:“今天下午 5:34”。 Current value: ''.当前值:''。

How can i fix this error?我该如何解决这个错误? Please help.请帮忙。

if like in angular, try to use如果喜欢角度,请尝试使用

this._changeDetectionRef.detectChanges();

at the end of your method, not forgetting to add在你的方法结束时,不要忘记添加

private _changeDetectionRef : ChangeDetectorRef

as parameter of the constructor of the Component owning your method.作为拥有您的方法的组件的构造函数的参数。

See discution here在这里查看讨论

Could you try this你能试试这个吗

lastdate:any;                    
formatEpoch(epoch): string {
   setTimeout(()=> {
     if(epoch == this.lastdate){
        return '';
     }else{ 
       this.lastdate =epoch;
       return UtilService.getCalendarDay(epoch);
     }
   }, 100);
}

the best solution:最佳解决方案:

  import { Platform } from '@ionic/angular';

  // ...

  constructor(
    ...
    private _platform:Platform
  ) { }

  ngOnInit() {
    this._platform.ready().then(() => {
      this.formatEpoch()
    })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM