英文:
How to use animations in an angular standalone component
问题
I'm trying to setup an angular project, standalone component + animations:
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule, BrowserAnimationsModule],
template: `
<div [@stagger]>
<div *ngFor="let item of items; trackBy:identify">{{ item.value }}</div>
</div><button (click)="onClick()">Update</button>`,
animations: [
trigger('stagger', [
transition('* => *', [
query(
':enter',
[
style({ opacity: 0 }),
stagger(100, [animate('0.5s', style({ opacity: 1 }))]),
],
{ optional: true }
),
]),
]),
],
})
export class App {
items = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }];
onClick() {
this.items = [...this items, { value: 99 }];
}
identify(index, item) {
return item.value;
}
}
bootstrapApplication(App);
However, I get the following error
ERROR
Error: Providers from the `BrowserModule` have already been loaded. If you need access to common directives such as NgIf and NgFor, import the `CommonModule` instead.
Without animations (without `BrowserAnimationsModule`) it works. But as soon as I want to use animations, I have to import `BrowserAnimationsModule` and it all breaks. Any suggestions?
[DEMO][1]
英文:
I'm trying to setup an angular project, standalone component + animations:
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule, BrowserAnimationsModule],
template: `
<div [@stagger]>
<div *ngFor="let item of items; trackBy:identify">{{ item.value }}</div>
</div><button (click)="onClick()">Update</button>`,
animations: [
trigger('stagger', [
transition('* => *', [
query(
':enter',
[
style({ opacity: 0 }),
stagger(100, [animate('0.5s', style({ opacity: 1 }))]),
],
{ optional: true }
),
]),
]),
],
})
export class App {
items = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }];
onClick() {
this.items = [...this.items, { value: 99 }];
}
identify(index, item) {
return item.value;
}
}
bootstrapApplication(App);
However, I get the following error
ERROR
Error: Providers from the `BrowserModule` have already been loaded. If you need access to common directives such as NgIf and NgFor, import the `CommonModule` instead.
Without animations (without BrowserAnimationsModule
) it works. But as soon as I want to use animations, I have to import BrowserAnimationsModule
and it all breaks. Any suggestions?
答案1
得分: 7
你需要添加作为提供者 provideAnimations
bootstrapApplication(App, {
providers: [
provideAnimations()
]
});
不要导入 BrowserAnimationsModule
注意:我不太确定你的动画,一个示例(看看关于 "*ngFor" 中的 divs
template: `
<div >
<div [@stagger] *ngFor="let item of items; trackBy:identify">{{ item.value }}</div>
</div><button (click)="onClick()">Update</button>`,
animations: [
trigger('stagger',[
transition(':enter', [
style({ opacity: 0 }),
animate('100ms', style({ opacity: 1 })),
]),
transition(':leave', [
animate('100ms', style({ opacity: 0 }))
])
])
],
注意2:当我们使用独立组件时,有一些提供者可以在必要时声明。请参考这个关于 配置 Angular 环境 的链接
英文:
you need add as provider provideAnimations
bootstrapApplication(App, {
providers: [
provideAnimations()
]});
not import BrowserAnimationsModule
NOTE: I'm not pretty sure about your animation, An e.g. (see that is about the "divs in the *ngFor"
template: `
<div >
<div [@stagger] *ngFor="let item of items; trackBy:identify">{{ item.value }}</div>
</div><button (click)="onClick()">Update</button>`,
animations: [
trigger('stagger',[
transition(':enter', [
style({ opacity: 0 }),
animate('100ms', style({ opacity: 1 })),
]),
transition(':leave', [
animate('100ms', style({ opacity: 0 }))
])
])
],
Your forked stackblitz
NOTE2: there're some providers we can declare when we work with standalone component if was necessary. See this link about configure Angular enviroment
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论