如果您在Angular 9中遇到了动画未触发的问题,可能原因是您没有导入正确的模块或动画关键帧未正确设置。
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({ imports: [BrowserAnimationsModule], ... }) export class AppModule { }
import { trigger, state, style, transition, animate } from '@angular/animations';
import { Component, OnInit } from '@angular/core'; import { trigger, state, style, transition, animate } from '@angular/animations';
@Component({ selector: 'app-my-component', templateUrl: './my-component.component.html', styleUrls: ['./my-component.component.css'], animations: [ trigger('fadeInOut', [ state('void', style({ opacity: 0 })), transition('* <=> *', animate('500ms ease-in-out')), ]), ], }) export class MyComponent implements OnInit { ... }
在上面的示例中,'fadeInOut”表示动画的名称,'void”状态表示动画的初始状态,它在该组件的初始加载时被触发。接下来,我们将设置状态和过渡动画。在这个例子中,'* <=> *” 表示任何状态到任何状态的变化都会触发动画。最后,我们设置动画类型为'ease-in-out”,持续时间为'500ms”。
通过这种方式,您应该能够在Angular 9中正确触发动画。