angular - 如何为 Angular 2 中的子组件设置动画?

标签 angular typescript animation

我尝试在父组件上添加动画,因此当子组件:enter 或:leave 时,会显示滑动效果。这是我的 parent @component:

  @component({
   selector: 'plan',
   templateUrl: '../templates/plan.tpl.html',
   styleUrls: ['../../../../assets/css/plan.scss'],
   animations: [
     trigger('stepAnimation', [
       transition(':enter', [
         style({transform: 'translateX(100%)'}),
         animate('0.5s ease-in-out', style({transform: 'translateX(0%)'}))
       ]),
    transition(':leave', [  // before 2.1: transition('* => void', [
      style({transform: 'translateX(0%)'}),
      animate('0.5s ease-in-out', style({transform: 'translateX(-100%)'}))
    ])
  ])]

})

然后我将动画选择器添加到模板中的子组件中,如下所示:

<start *ngIf="currentPage == 'start'" @stepAnimation></start>
<about *ngIf="currentPage == 'about'" @stepAnimation></about>
<family *ngIf="currentPage == 'family'" @stepAnimation></family>

动画不工作。然后我在每个组件中添加动画代码,并将@stepAnimation 添加到每个模板的父标签中。这样,我得到了进入动画但没有离开。我想那是因为父级上的 ngIf 但无论如何,有很多重复的动画代码。无论如何都可以在父级处理动画吗?

最佳答案

问题是自定义元素,<start> , <family> , 和 <about>没有样式,因此 display默认为 inline ,无法翻译。只需将其添加到您的父样式表中:

about, start, family {
    display: block;
    /*Any other layout code, e.g. position:absolute */
}

它应该可以工作。

关于angular - 如何为 Angular 2 中的子组件设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42263032/

相关文章:

angular - 如何在 Angular 中使用 formArray 修补 formGroup 中的值?

reactjs - 使用 Typescript 进行 Redux reducer 初始状态

jquery - Jquery中的随机数效果

c++ - Assimp 访问 aiNode 名称时出错

java - 我应该如何让我的玩家矩形跳跃?

typescript - Angular2(或 TypeScript)中的 "Declaration or statement expected"错误

javascript - 在 2017 年使用 Meteor 有充分的理由吗?

javascript - bootstrap 列上的 ngFor 和 ngIf

javascript - 如何使用 namespace 导入而不导入所有内容?

angular - 升级@ngrx/Store 时,类型 'payload' 上不存在属性 'Action'