【发布时间】:2020-02-19 20:34:38
【问题描述】:
我正在尝试提供服务以在加载时显示微调器进度,但控制台中出现错误
这里是服务:
@Injectable({
providedIn: 'root'
})
export class LoadingLockDataService {
public message: string;
public loaderType: string;
public value: number;
public sizeType = 'fullPage';
private overlayRef: OverlayRef = null;
constructor(public overlay: Overlay) {}
public setMessage(message: string): void {
this.message = message;
}
public showOverlay(component: any) {
if (!this.overlayRef) {
this.overlayRef = this.overlay.create();
}
const userProfilePortal = new ComponentPortal(component);
this.overlayRef.attach(userProfilePortal);
}
public hideOverlay() {
if (!!this.overlayRef) {
this.overlayRef.detach();
}
}
}
我试图在叠加层中动态渲染的组件:
<div class="spinner-wrapper" [ngClass]="{ fullPage: loadingLockDataService.sizeType === 'fullPage' }">
<div id="center-wrapper">
<div class="mat-spinner-wrapper">
<mat-progress-spinner class="spinner" strokeWidth="2" mode="indeterminate"> </mat-progress-spinner>
</div>
<label class="mat-body label-accent">
{{ loadingLockDataService.message }}
</label>
</div>
</div>
export class LoadingLockComponent implements OnInit {
constructor(public loadingLockDataService: LoadingLockDataService) {}
ngOnInit() {}
}
我在哪里调用服务:
ngOnInit() {
// setTimeout(() => {
this.loadingLockDataService.setMessage('Chargement en cours...');
this.loadingLockDataService.showOverlay(LoadingLockComponent);
// }, 1);
this.someService.subscribe(response => {
// do something
this.loadingLockDataService.hideOverlay();
});
}
我遇到以下错误: “错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。以前的值:'fullPage:undefined'。当前值:'fullPage:true'。看起来视图是在其父级及其子级经过脏检查后创建的。它是在变更检测钩子中创建的吗?”
而且我知道 setTimeout 在我调用服务的位置,它正在工作,但我想正确地做到这一点。我觉得是因为我渲染不好动态组件。
【问题讨论】:
标签: angular