在Angular 17中,

我父母有一个动画

Parent Component - animation configuration

 animations: [
    trigger('fadeInOut', [
      transition(':enter', [
        style({ opacity: 0 }),
        animate('150ms', style({ opacity: 1 })),
      ]),
      transition(':leave', [
        animate('150ms', style({ opacity: 0 })),
      ]),
    ]),
  ],

View html element - fully works

当我将它应用到模板中的html时,它工作得很好:

   @if(showIcon) {

      <span class="material-symbols-outlined" [@fadeInOut]>
        check_circle
      </span>

    }

Child Component - half works (on fadeIn only)

当应用到子组件时,它只在淡出时起作用,当外部@ if中的布尔值切换回false时,它会立即剪切出来,而不是淡出.

    @if(showChildComponent){
      <app-child-component
        [@fadeInOut]
        [someInput]="Input"
        (someEvent)="handleEvent($event)"/>
    }
  }

这是一个bug,还是我的方法错了?

推荐答案

您的代码运行正常,也许您缺少添加的东西,请参考下面的stackblitz,请复制问题并分享回来,如果这个答案不起作用!

import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideAnimations } from '@angular/platform-browser/animations';
import 'zone.js';
import { ChildComponent } from './app/child/child.component';
import { trigger, transition, style, animate } from '@angular/animations';
@Component({
  selector: 'app-root',
  standalone: true,
  animations: [
    trigger('fadeInOut', [
      transition(':enter', [
        style({ opacity: 0 }),
        animate('150ms', style({ opacity: 1 })),
      ]),
      transition(':leave', [animate('150ms', style({ opacity: 0 }))]),
    ]),
  ],
  imports: [ChildComponent],
  template: `
    @if(showChildComponent){
      <app-child
        [@fadeInOut]/>
    }
  `,
})
export class App {
  name = 'Angular';
  showChildComponent = false;

  ngOnInit() {
    setInterval(() => {
      this.showChildComponent = !this.showChildComponent;
    }, 1500);
  }
}

bootstrapApplication(App, {
  providers: [provideAnimations()],
});

Stackblitz Demo

Angular相关问答推荐

如何避免使用CSS动画制作的幻灯片中闪烁?

接收错误NullInjectorError:R3InjectorError(_AppModule)[config—config]....>过度安装Angular—Markdown—Editor

有没有一种方法用timeout()操作符创建计数器,用于超时一个观察对象?

如何使用formBuilder.Group()从角形表单中删除选定的选项?

弯管记忆

如何在Angular功能路由保护中取消订阅RxJs主题

使用 primeng Apollo 主题进行实时部署时多次加载 theme.css 和 preloading.css 文件

将 autoSpy 与 ng-mocks 一起使用时,如何重置 jasmine 间谍调用?

最新版本的 Chrome ERR_TIMED_OUT

如何为所有模块全局声明指令?

Angular 2 - 事件列表

ViewChild 和 ContentChild 的所有有效 Select 器是什么?

Angular 7 测试:NullInjectorError:No provider for ActivatedRoute

在 Angular 中动态设置样式

如何为 Angular 2 中的渲染元素绑定事件监听器?

如何在悬停时向元素添加类?

Angular2material对话框自动关闭

测试一个包含 setTimeout() 的函数

使用 EventEmitter 传递参数

如何在Angular 2中将对象从一个组件传递到另一个组件?