The @Component
annotation provides us with an animations
property. This can be used to define a list of triggers
each with a lot of state
and transition
properties.
When you add multiple animations to a component, this list can become pretty long. Also some animations would be really nice to use in other components as well. Having to put them directly in each component seems tedious and is repetitive - plus it violates the DRY principle.
You can define the template and styles properties on your component as well, but here you have the option of providing a templateUrl
and styleUrls
instead. I can't seem to find an animationUrls
property - am i missing something - is there a way to do this?
Sure you can. You can just declare the animation in a different file, then import it where you need it
animations.ts
component.ts
Or if you want to make it configurable, you can export a function. For example, take a look at the Fuel-UI Collapse. This is a reusable (and configurable) animation
collapse.ts
Then in your components, just use
You certainly and the gentlemen has done so in some of his github repo examples. Take the following:
route_animations.ts
Which is then imported into a component like the following:
And then called like this under the animation Param when initialising the component:
Take a look at this yourself to get a better idea but I hope this helps. https://github.com/matsko/ng2eu-2016-code/blob/master
Kudos to matsko.
Sure that's possible. You can make for instance an
animations.ts
and let it export all kind of animation constants:and in your component you can import this animation using the
import
statement:and apply it to your component:
Here's another example fade in animation in Angular 4 I use for animating routes
And a component with the transition attached
More details and a live demo at this post