Answer the question
In order to leave comments, you need to log in
How to fix ExpressionChangedAfterItHasBeenCheckedError when using router.events and navigate?
Implemented a transition spinner between child components using router.events.
Main Component Template
<div [ngClass]="{'loading-class':loading}">
<router-outlet></router-outlet>
</div>
export class MyComponent {
loading: boolean;
constructor(router: Router) {
router.events
.pipe(
filter(e => e instanceof NavigationStart || e instanceof NavigationEnd),
map(e => e instanceof NavigationStart),
distinctUntilChanged()
)
.subscribe(loading => this.loading = loading);
}
}
Answer the question
In order to leave comments, you need to log in
export class MyComponent {
loadingClass = this.createLoadingClass();
constructor(router: Router) {}
private createLoadingClass(): Observable<boolean> {
return router.events.pipe(
map(e => e instanceof NavigationStart),
);
}
}
<div [class.loading]="loadingClass | async">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question