获取ExpressionChangedAfterItHasBeenCheckedError

时间:2019-07-09 05:47:44

标签: angular asynchronous service rxjs

在多个位置使用订阅行为对象以进行加载的组件时获得ExpressionChangedAfterItHasBeenCheckedError。将组件加载到一个组件中并尝试在另一组件中loaddestroy时发生错误。

@Component({
template: 'common.component.html',
selector: 'app-common'
})

export CommonComponent implements OnInit{

constructor(service: MyService){}

ngOnInit(){
}

}

**common.component.html**

<div>
    <div *ngIf="service.isLoading$ | async">Loading...</div>
</div>


**component 1**

<div>
    <app-common></app-common>
</div>

component 2

<div>
    <app-common></app-common>
</div>


@Injectable()
export class MyService {
  public isLoading = new BehaviorSubject<any>(false);
  public isLoading$ = this.isLoading.asObservable();
}```

2 个答案:

答案 0 :(得分:0)

public isLoading$ = this.isLoading.asObservable().pipe(delay(0))

更多信息

https://blog.angular-university.io/angular-debugging/

答案 1 :(得分:0)

使用ChangeDetectionStrategy.OnPush

import { ChangeDetectionStrategy } from '@angular/core';

@Component({template: 'common.component.html',
   selector: 'app-common',
   changeDetection: ChangeDetectionStrategy.OnPush
})