let's say I have 2 routed components and two Routerlinks in the fixed navbar to route them. I want them to slide in from the right when I click the Routerlinks.
I don't want to offset the component with css and use a timeout function to change the css class to let it slide in (e.g. with ngStyle or ngClass).
are there any more elegant ways do achieve that in Angular 2?
Thanks!
With Angular 4.1 it is now possible to create specific route animations. This is different from triggering an animation when a component is displayed because it will let you animate the entering/leaving component at the same time for a smooth transition, and let you modify the transition depending on which component is coming or going. That means you can do complex transitions like slide a component in from the right if you're drilling down into content, and slide it in from the left if you're entering it via a 'back' button from another component.
First, annotate your router outlet like so (eg.
app.component.html
):Implement the
prepareRouteTransition(outlet)
function in the corresponding component definition (e.g.app.component.js
).Define your animations (e.g.
app.component.js
):Add the animation metadata to your route definitions (e.g.
app.routing.ts
):Finally, register a 'routerAnimations' animation trigger on your component with the animations and route metadata you defined (e.g.
app.component.js
):Don't forget to polyfill the Web Animation API to target old browsers
Matias Niemela talks more about route animations at ng-conf here (with a demo): https://youtu.be/Oh9wj-1p2BM?t=12m21s
His presentation code: https://github.com/matsko/ng4-animations-preview
In terms of sliding in it is quite straightforward.
You can reference to the Official Angular 2 Animate docs.
You can also check out this Plunker I did for a simple showcase, using the new router v3
Bear in mind that I am struggling to figure out how to actually have the leave/exit/void transitions when the triggered element is about to be destroyed from the view.
I opened another thread in Angular 2 Animate - No visible effect of the '* => void' transition when changing routes/components to try to figure out how to make router take notice of the leaving animation/transition time.