编辑检查这个 stackblitz 工作示例
import { ...} from '@angular/core';
import {trigger, state, style, animate, transition} from '@angular/animations';
@Component({
selector: 'my-app',
animations: [
// Each unique animation requires its own trigger. The first argument of the trigger function is the name
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 class RotateComponent {
state: string = 'default';
rotate() {
this.state = (this.state === 'default' ? 'rotated' : 'default');
}
}
在模板中:
<button (click)="rotate()">Press me to rotate</button>
并确保将绑定添加到标签:
[@rotatedState]='state'
此外,请确保将动画模块导入到您的应用程序中,如下所示:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
...,
imports: [
...,
BrowserAnimationsModule
],
...
})