如何在Angular独立组件中使用动画。

huangapple go评论68阅读模式
英文:

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: &#39;my-app&#39;,
    standalone: true,
    imports: [CommonModule, BrowserAnimationsModule],
    template: `
        &lt;div [@stagger]&gt;
            &lt;div *ngFor=&quot;let item of items; trackBy:identify&quot;&gt;{{ item.value }}&lt;/div&gt;
        &lt;/div&gt;&lt;button (click)=&quot;onClick()&quot;&gt;Update&lt;/button&gt;`,
    animations: [
        trigger(&#39;stagger&#39;, [
            transition(&#39;* =&gt; *&#39;, [
                query(
                   &#39;:enter&#39;,
                   [
                       style({ opacity: 0 }),
                       stagger(100, [animate(&#39;0.5s&#39;, 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);

DEMO

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 }))
    ])
  ])
],

你的 forked stackblitz

注意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: `
  &lt;div &gt;
    &lt;div [@stagger] *ngFor=&quot;let item of items; trackBy:identify&quot;&gt;{{ item.value }}&lt;/div&gt;
  &lt;/div&gt;&lt;button (click)=&quot;onClick()&quot;&gt;Update&lt;/button&gt;`,
  animations: [
    trigger(&#39;stagger&#39;,[
      transition(&#39;:enter&#39;, [
        style({ opacity: 0 }),
        animate(&#39;100ms&#39;, style({ opacity: 1 })),
      ]),
      transition(&#39;:leave&#39;, [
        animate(&#39;100ms&#39;, 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

huangapple
  • 本文由 发表于 2023年2月10日 02:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定