I have set up a slide animation in Angular 7 when changing the route. The problem I have right now is that the animation is stuttering, because the Component that I'm navigating to is executing code during the animation in the OnInit
lifecycle.
How do I initialize code for the component after the animation has finished to prevent frame drops?
Edit:
I'm using router animations, here's my setup:
app.component.html
:
<div [@routeAnimations]="prepareRoute(outlet)">
<router-outlet #outlet="outlet"></router-outlet>
</div>
app.component.ts
:
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
animations: [routeAnimations]
})
export class AppComponent {
prepareRoute(outlet: RouterOutlet) {
return outlet && outlet.activatedRouteData && outlet.activatedRouteData.animation;
}
}
app-routing.module.ts
:
const routes: Routes = [
{
path: 'sign-in',
loadChildren: './modules/sign-in/sign-in.module#SignInModule',
data: {animation: 'slideLeft'}
},
{
path: 'dashboard',
component: DashboardComponent,
data: {animation: 'slideRight'}
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
animations.ts
:
export const routeAnimations =
trigger('routeAnimations', [
transition('slideLeft => slideRight', [
// ...a bunch of animation code...
]),
transition('slideRight => slideLeft', [
// ...a bunch of animation code...
])
]);