你能把你的动画移动到 Angular2 中的外部文件吗?

Can you move your animations to an external file in Angular2?

@Component注解为我们提供了animations属性。这可用于定义 triggers 的列表,每个列表都有很多 statetransition 属性。

当您向一个组件添加多个动画时,此列表可能会变得很长。此外,一些动画也非常适合在其他组件中使用。必须将它们直接放在每个组件中似乎很乏味且重复 - 而且它违反了 DRY 原则。

您也可以在组件上定义模板和样式属性,但在这里您可以选择提供 templateUrlstyleUrls。我似乎找不到 animationUrls 属性 - 我是否遗漏了什么 - 有办法做到这一点吗?

当然可以。您可以只在不同的文件中声明动画,然后将其导入到您需要的地方

animations.ts

export const animation = trigger(...)

component.ts

import { animation } from './animations'

@Component({
  animations: [ animation ]
})

或者如果你想让它可配置,你可以导出一个函数。例如,看看 Fuel-UI Collapse。这是一个可重用(和可配置)的动画

collapse.ts

export function Collapse(duration: number = 300) {
    return trigger('collapse', [
           ...
        ])
}

然后在你的组件中,使用

import { Collapse } from './collapse'

@Component({
  animations: [ Collapse(300) ],
  template: `
    <div @collapse="collapsed ? 'true' : 'false'">
    </div>
  `
})
class MyComponent {}

当然可以。例如,您可以制作一个 animations.ts 并让它导出所有类型的动画常量:

export const PRETTY_ANIMATION = trigger('heroState', [
  state('inactive', style({
    backgroundColor: '#eee',
    transform: 'scale(1)'
  })),
  state('active',   style({
    backgroundColor: '#cfd8dc',
    transform: 'scale(1.1)'
  })),
  transition('inactive => active', animate('100ms ease-in')),
  transition('active => inactive', animate('100ms ease-out'))
])

并且在您的组件中,您可以使用 import 语句导入此动画:

import {PRETTY_ANIMATION} from 'animations';

并将其应用于您的组件:

@Component({
    selector : `...`
    animations : [PRETTY_ANIMATION]
})

您当然和先生们已经在他的一些 github 回购示例中这样做了。采取以下措施:

route_animations.ts

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

var startingStyles = (styles) => {
    styles['position'] = 'fixed';
    styles['top'] = 0;
    styles['left'] = 0;
    styles['right'] = 0;
    styles['height'] = '100%';
    return styles;
}

export default function(name) {
    return trigger(name, [
        transition('void => *', [
            style(startingStyles({
                transform: 'translateX(100%)'
            })),
            animate('0.5s ease-out', style({ transform: 'translateX(0%)'}))
        ]),
        transition('* => void', [
            style(startingStyles({
                transform: 'translateX(0%)'
            })),
            animate('0.5s ease-in', style({ transform: 'translateX(-100%)'}))
        ])
    ]);
}

然后将其导入到如下组件中:

import {default as routerAnimations} from '../route_animations';

然后在初始化组件时在动画Param下这样调用:

animations: [routerAnimations('route')],

自己看看这个以获得更好的想法,但我希望这对您有所帮助。 https://github.com/matsko/ng2eu-2016-code/blob/master

向 matsko 致敬。

这是 Angular 4 中动画淡入淡出的另一个例子,我用它来制作动画路线

// import the required animation functions from the angular animations module
import { trigger, state, animate, transition, style } from '@angular/animations';
 
export const fadeInAnimation =
    // trigger name for attaching this animation to an element using the [@triggerName] syntax
    trigger('fadeInAnimation', [
 
        // route 'enter' transition
        transition(':enter', [
 
            // css styles at start of transition
            style({ opacity: 0 }),
 
            // animation and styles at end of transition
            animate('.3s', style({ opacity: 1 }))
        ]),
    ]);

以及附加了过渡的组件

import { Component } from '@angular/core';
 
// import fade in animation
import { fadeInAnimation } from '../_animations/index';
 
@Component({
    moduleId: module.id.toString(),
    templateUrl: 'home.component.html',
 
    // make fade in animation available to this component
    animations: [fadeInAnimation],
 
    // attach the fade in animation to the host (root) element of this component
    host: { '[@fadeInAnimation]': '' }
})
 
export class HomeComponent {
}

更多详细信息和现场演示,请访问 this post