I'm currently experimenting with Angular7 route transition animations and have the following problem:
In my app there is a entry-component which has its own routing module and <router-outlet>
.
What I'm trying to achieve is that whenever the route changes the old displayed component fades away (opacity: 0
) before the new component fades in. But unfortunately I can't seem to make it work.
The animations don't play at all, and debugging as explained in the angular docs:Animations transition and triggers
(@routeAnimation.start)="onAnimationEvent($event)"
(@routeAnimation.done)="onAnimationEvent($event)"
shows that the animation is only triggered once (on pageload; and even then it doesn't play) but not while I use my navigator-component to navigate through the app.
My code looks like this:
entry.component.ts
@Component({
selector: 'entry',
templateUrl: './entry.component.html',
styleUrls: ['./entry.component.css'],
animations: [routeAnimation],
})
export class EntryComponent implements OnInit {
constructor() { }
ngOnInit() { }
prepareRoute(outlet: RouterOutlet) {
const animation = outlet.activatedRouteData['animation'] || {};
return animation['value'] || 'WelcomePage';
}
}
entry.component.html
<navigator>
</navigator>
<div [@routeAnimation]="prepareRoute(o)" id="entry-content">
<router-outlet #o="outlet" name="entry"></router-outlet>
</div>
entry-routing.module.ts
const routes: Routes = [
{
path: 'entry',
component: EntryComponent,
children: [
{
path: '',
component: WelcomeComponent,
outlet: 'entry',
data: { animation: 'WelcomePage' }
},
{
path: 'introduction',
component: IntroductionComponent,
outlet: 'entry',
data: { animation: 'IntroductionPage' }
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class EntryRoutingModule { }
route.animation.ts
export const routeAnimation = trigger(
'routeAnimation', [
transition('* => *', [
group([
query(':enter', style({ opacity: 0 })),
query(':leave', [
animate('0.5s', style({ opacity: 0 })),
style({ display: 'none' })
], { optional: true }),
query(':enter', [
animate('2s',
style({ opacity: 1 })),
animateChild()
], { optional: true })
])
]),
]
);
Any Ideas what's missing/what I do wrong? Help is greatly appreciated and many thanks in advance!