【问题标题】:Angular 11, :enter and :leave animation not working with *ngIfAngular 11、:enter 和 :leave 动画不适用于 *ngIf
【发布时间】:2021-07-25 00:04:41
【问题描述】:

我刚开始使用 Angular 动画。并且不想用它为 *ngIf 设置动画。遗憾的是它不起作用:(。我没有收到错误消息。我从这里尝试了几种解决方案,但没有任何效果。此外,删除其中一个动画不会改变任何内容,或者完全删除其中一个 *ngIf-blocks 不会'不要改变任何东西。它只是不起作用,并且终端或可见的开发工具中也没有错误。

这里是打字稿中的动画定义。

animations: [
  trigger('inOutPaneAnimation', [
    state('true', style({ opacity: 1, transform: 'translateX(0)' })),
  state('void', style({ opacity: 0, transform: 'translateX(-100%)' })),
  transition(':enter', [animate('750ms ease-in-out')]),
  transition(':leave', [animate('600ms ease-in-out')]),
]),
trigger('inOutActiveItemAnimation', [
    state('true', style({ opacity: 1, transform: 'translateX(0)' })),
  state('void', style({ opacity: 0, transform: 'translateX(100%)' })),
  transition(':enter', [animate('600ms ease-in-out')]),
  transition(':leave', [animate('750ms ease-in-out')]),
]),

HTML 如下所示:

<div
  *ngIf="activeItem"
  [@inOutActiveItemAnimation]
  class="bt3-locations__active-item"
>
  <app-location-active-item
    [isSingleLocation]="isSingleLocation"
    [location]="activeItem"
    [showRouteLabel]="showRouteLabel"
  ></app-location-active-item>
</div>
<div *ngIf="!activeItem" [@inOutPaneAnimation] #paneContent>
  <div
    *ngTemplateOutlet="
      locations.data.length > 1 ? multipleGroups : group;
      context: { data: getGroups(), group: 0 }
    "
  ></div>
</div>

BrowserAnimationsModule 被导入到父模块中。

import { CommonModule } from '@angular/common';
import { CUSTOM_ELEMENTS_SCHEMA, Injector, NgModule } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { LocationItemComponent } from './location-item/location-item.component'; 
import { LocationsComponent } from './locations/locations.component';
import { LocationActiveItemComponent } from './location-active-item/location-active- 
item.component';
import { AccordionModule } from '../accordion/accordion.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
    declarations: [
      LocationsComponent,
    LocationItemComponent,
    LocationActiveItemComponent,
  ],
  imports: [CommonModule, AccordionModule, BrowserAnimationsModule],
  exports: [],
  providers: [],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
})

【问题讨论】:

    标签: angular angular-animations angular11


    【解决方案1】:

    这就是您创建 :enter:leave 动画的方式

    trigger("inOutPaneAnimation", [
        transition(":enter", [
            style({ opacity: 0, transform: "translateX(-100%)" }), //apply default styles before animation starts
            animate(
                "750ms ease-in-out",
                style({ opacity: 1, transform: "translateX(0)" })
            )
        ]),
        transition(":leave", [
            style({ opacity: 1, transform: "translateX(0)" }), //apply default styles before animation starts
            animate(
                "600ms ease-in-out",
                style({ opacity: 0, transform: "translateX(-100%)" })
            )
        ])
    ])
    

    你甚至不需要属性绑定[],这就够了@inOutPaneAnimation

    <div *ngIf="!activeItem" @inOutPaneAnimation #paneContent>
    ...
    </div>
    

    Read More Here

    这是working stackblitz example

    注意:确保在主模块中导入BrowserAnimationsModule

    【讨论】:

    • 可悲的是,还没有工作。那是我最初遵循的方式,然后我确实摆弄了一下。但是对于您的代码,它也不起作用。当 *ngIf 未绑定到布尔值时,动画不起作用可能是一个问题吗?添加了 ng-trigger-inOutPaneAnimation 类,所以我认为这是可行的。
    • @MarcelBührig 答案已更新,添加了工作示例
    • BrowserAnimationsModule 被导入到主模块中。我们还有其他工作代码,其中有动画。只是不要 :enter 和 :leave 动画。
    • 您的解决方案有效。您不知道也无法知道顶部元素位于 ViewEncapsulation.ShadowDom 上,那么它就不起作用了。
    【解决方案2】:

    解决方案相当简单。

    确保您的组件的 ViewEncapsulation 未设置为 ShadowDom。然后就不行了。

    还要确保检查@Sameer 的答案。这是实现它的正确方法。

    【讨论】:

    • 你自己修好了,我不知道,直到现在还没有修复。这是issue
    猜你喜欢
    • 2021-11-11
    • 2020-03-04
    • 2015-08-23
    • 2017-02-22
    • 2020-06-28
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2019-08-13
    相关资源
    最近更新 更多