多个 Angular 组件的可重用动画?

One reusable animation for multiple Angular components?

我在我的 Angular 应用程序中使用动画,许多组件使用相同的动画,但为此我 copy/paste 每个组件中的动画。我们能否重用动画而不将其应用于单个组件。

你应该创建一个animations.ts文件,并在每个组件中导入你想要的动画:

例子animations.ts

import {
  trigger,
  style,
  animate,
  transition,
  state,
  group
} from '@angular/animations';


export const rotations = [
  trigger('rotatedState', [
    state('default', style({
      transform: 'rotate(0)'
    })),
    state('rotated', style({
      transform: 'rotate(-180deg)'
    })),
    transition('rotated => default',
      animate('400ms ease-out')
    ),
    transition('default => rotated',
      animate('400ms ease-in')
    )
  ])
];

export const someOtherAnimations = [
  ...
];

现在在 component.ts 中导入:

import { rotations } from 'app/animations';

@Component({
  ...
  animations: [rotations],
})

现在您可以在 component.html 中使用:

<img @rotatedState >