【发布时间】:2019-10-23 19:41:00
【问题描述】:
我正在尝试在组件上设置一个 fadeInOut 动画。 我的应用模块导入 BrowserAnimationsModule。
我在单独的文件中创建了一个动画和一个触发器:
import { animate, style, animation, trigger, useAnimation, transition } from '@angular/animations';
export const fadeIn = animation([style({ opacity: 0 }), animate('500ms', style({ opacity: 1 }))]);
export const fadeOut = animation(animate('500ms', style({ opacity: 0 })));
export const fadeInOut = trigger('fadeInOut', [
transition('void => *', useAnimation(fadeIn)),
transition('* => void', useAnimation(fadeOut))
]);
然后,我创建了一个组件并验证了该组件本身是否有效:
import { Component, OnInit } from '@angular/core';
import { Globals } from '@app/globals';
import { fadeInOut } from '@app/animations';
@Component({
selector: 'app-global-alert',
template: `
<div class="global-alert" *ngIf="globalAlert">
<div class="global-alert-message"><ng-content></ng-content></div>
<div class="close" (click)="closeGlobalAlert()"></div>
</div>
`,
styles: [],
animations: [fadeInOut]
})
export class GlobalAlertComponent implements OnInit {
private globalAlert: boolean;
constructor(private globals: Globals) {
this.globalAlert = globals.hasGlobalAlert;
}
ngOnInit() {}
closeGlobalAlert() {
this.globals.hasGlobalAlert = false;
this.globalAlert = false;
}
}
请注意,我正在存储此警报是否应出现在 globals.ts 文件中的状态,尽管这无关:
import { Injectable } from '@angular/core';
@Injectable()
export class Globals {
hasGlobalAlert = true;
}
所以我在另一个组件的 html 中使用该组件,如下所示:
<div>
lots of html
</div>
<app-global-alert>Hello world</app-global-alert>
这可行,当您单击关闭按钮时,警报会消失,一切都按预期工作。但是,当我尝试将触发器添加到它时
<app-global-alert [@fadeInOut]>Hello world</app-global-alert>
我收到控制台错误
Error: Found the synthetic property @fadeInOut. Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.
我已经用谷歌搜索过这个,但我已经在大多数回复中涵盖了所有问题:我在组件中包含了 animations 声明,等等。
我错过了什么?
【问题讨论】:
标签: javascript angular animation angular-animations