简体   繁体   English

在检查app.component.ts中的错误后,角度4表达式已更改

[英]angular 4 Expression Changed After It Has Been Checked Error in app.component.ts

I get the ExpressionChangedAfterItHasBeenCheckedError. Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'. 我得到ExpressionChangedAfterItHasBeenCheckedError. Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'. ExpressionChangedAfterItHasBeenCheckedError. Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'. error after I log out somewhere inside the app, and the front getter in app.component.ts changes in its view. 我在应用程序内的某个地方注销后出现错误,并且app.component.tsfront app.component.ts在其视图中app.component.ts变化。

Also, if I enable production mode, the error does not appear! 另外,如果启用生产模式,则不会出现该错误! It all works regardless, but I would like not to see the error. 一切都可以,但是我不想看到该错误。

app.component.html app.component.html

<div class="col-xs-12" [class.space]="!front">
            <router-outlet></router-outlet>
        </div>

app.component.ts app.component.ts

get front() {
        if (localStorage.getItem("user") === "\"admin\"") {
            return false;
        }
        return true;
    }

put the whole code inside a setTimeout like this 将整个代码放在这样的setTimeout

get front() {
  return new Promise(resolve => setTimeout(() => {
    if (localStorage.getItem("user") === "\"admin\"") {
      resolve(false);
    }
    resolve(true);
  }, 200));
}

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

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