简体   繁体   English

角度加载按钮ExpressionChangedAfterItHaHasBeenCheckedError

[英]Angular loading button ExpressionChangedAfterItHasBeenCheckedError

I have this component I use in my application and for the most part it works fine. 我有我在应用程序中使用的组件,并且在大多数情况下都可以正常工作。 It's ts file looks like this: ts文件看起来像这样:

import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { Router } from '@angular/router';

import { LoadingService } from '@pyb-services/loading.service';

@Component({
  selector: 'pyb-loading-button',
  templateUrl: './loading-button.component.html',
  styleUrls: ['./loading-button.component.scss']
})
export class LoadingButtonComponent implements OnInit, OnChanges {
  @Input() link: any[] = []
  @Input() target: string
  @Input() navigateAfter: boolean
  clicked: boolean = false
  loading: boolean = false; // Has to be false by default, because sometimes everything has loaded before this button has come on the page

  constructor(
    private router: Router,
    private loadingService: LoadingService
  ) {
  }

  ngOnInit() {
    this.loadingService.onLoadingChanged.subscribe(isLoading => this.loading = isLoading);
  }

  ngOnChanges() {
    this.navigateWhen();
  }

  click(): void {
    this.clicked = !this.clicked;
    this.navigateWhen();
  }

  private navigateWhen(): void {
    if ((!this.loading || this.navigateAfter) && this.clicked && this.link.length) {
      this.router.navigate(this.link); 

      this.clicked = false;
    }
  }
}

And the template file looks like this: 模板文件如下所示:

<button class="btn btn-xl btn-secondary" type="button" [disabled]="loading && !navigateAfter" (click)="click()" [scrollTo]="target"
  offset="10" [ngSwitch]="loading">
  <span class="spinner" *ngSwitchCase="true">
    <span class="bounce1"></span>
    <span class="bounce2"></span>
    <span class="bounce3"></span>
  </span>
  <span *ngSwitchDefault>Next step</span>
</button>

Like I have said, it seems to work fine. 就像我说的那样,它似乎工作正常。 But, if another process kicks off an http request, then I get the error: 但是,如果另一个进程启动了http请求,那么我会收到错误消息:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: 'ngSwitch: false'. 先前的值:'ngSwitch:false'。 Current value: 'ngSwitch: true'. 当前值:'ngSwitch:true'。

which is moaning about the loading property changing in the loading button component. 抱怨在加载按钮组件中更改了loading属性。 Does anyone know how I can fix this? 有谁知道我该如何解决?

Here is the issue with variable loading which is being changed while Change Detection has been trigger. 这是在触发变更检测时正在更改的变量loading的问题。 To avoid this error set the value of loading in separate thread. 为避免此错误,请在单独的线程中设置loading值。

  ngOnInit() {
    this.loadingService.onLoadingChanged.subscribe(isLoading => 
       Promise.resolve(null).then(() => this.loading = isLoading)
    );
  }

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

相关问题 Angular 加载错误 ExpressionChangedAfterItHasBeenCheckedError - Angular Loading Error ExpressionChangedAfterItHasBeenCheckedError 角度加载微调框错误:ExpressionChangedAfterItHaHasBeenCheckedError - Angular Loading Spinner Error:ExpressionChangedAfterItHasBeenCheckedError Angular动态组件加载 - ExpressionChangedAfterItHasBeenCheckedError - Angular dynamic component loading - ExpressionChangedAfterItHasBeenCheckedError 创建Angular4“正在加载”屏幕:ExpressionChangedAfterItHaHasBeenCheckedError - Creating Angular4 'Loading' Screen: ExpressionChangedAfterItHasBeenCheckedError Angular:尝试禁用按钮时出现 ExpressionChangedAfterItHasBeenCheckedError - Angular: ExpressionChangedAfterItHasBeenCheckedError when trying to disable button ExpressionChangedAfterItHasBeenCheckedError:角度 - ExpressionChangedAfterItHasBeenCheckedError: angular ExpressionChangedAfterItHasBeenCheckedError Angular - ExpressionChangedAfterItHasBeenCheckedError Angular Angular 4 ExpressionChangedAfterItHasBeenCheckedError - Angular 4 ExpressionChangedAfterItHasBeenCheckedError 使用单选按钮组打开 Angular 模态 window 会导致 ExpressionChangedAfterItHasBeenCheckedError - Opening an Angular modal window with a Radio Button group causes ExpressionChangedAfterItHasBeenCheckedError ExpressionChangedAfterItHasBeenCheckedError /角4角7 - ExpressionChangedAfterItHasBeenCheckedError / Angular 4-Angular 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM