简体   繁体   English

如何在Angular2中将动画应用于具有基本组件的多个组件?

[英]How to apply animations to multiple components with a base component in Angular2?

In my app when a component loads from menu using routing, I wanted to use some animations. 在我的应用中,当组件使用路由从菜单加载时,我想使用一些动画。 I was able to do it with an angular animation successfully. 我能够成功地用角度动画做到这一点。 I had to apply the animation in the individual component to achieve this. 我必须在单个组件中应用动画才能实现此目的。 I need to know whether there is a better way to apply animations to a group of components or components inheriting from a specific base class? 我需要知道是否有更好的方法将动画应用于一组组件或从特定基类继承的组件?

Currently I have applied my animation like, 目前,我已将自己的动画应用于

import { Component, OnInit } from '@angular/core';
import { routerTransition } from "../animations/animations";

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.css'],
  animations: [routerTransition()],
  host: {'[@routerTransition]': ''}
})
export class BaseComponentComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

animation code, 动画代码

import {trigger, state, animate, style, transition} from '@angular/core';

export function routerTransition() {
  return slideToLeft();
}

function slideToLeft() {
  return trigger('routerTransition', [
    state('void', style({position:'fixed', width:'100%'}) ),
    state('*', style({position:'fixed', width:'100%'}) ),
    transition(':enter', [  // before 2.1: transition('void => *', [
      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%)'}))
    ])
  ]);
}

What I tried: 我试过的

I tried to create a base-component, applied animation properties there, and extended the base-component in the other components needed. 我尝试创建一个基础组件,在其中应用了动画属性,并在所需的其他组件中扩展了基础组件。 Animation stopped working after that. 此后动画停止工作。

Please let me know if we can reuse the animations without applying it on individual components. 请让我知道是否可以在不将动画应用于单个组件的情况下重复使用它们。

Check this tutorial , is very good explained how to use animations: For animations on multiple elements you should rather use many triggers with different states. 检查本教程 ,很好地解释了如何使用动画:对于多个元素上的动画,您应该使用状态不同的许多触发器。 check the video. 检查视频。

Since version 4.2, Angular supports animating multiple elements via the newer query() and stagger() features in the animation DSL. 从4.2版开始,Angular支持通过动画DSL中较新的query()stagger()功能对多个元素进行动画处理。

This article explains the new features: https://www.yearofmoo.com/2017/06/new-wave-of-animation-features.html 本文介绍了新功能: https : //www.yearofmoo.com/2017/06/new-wave-of-animation-features.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM