【问题标题】:Can you move your animations to an external file in Angular2?您可以将动画移动到 Angular2 中的外部文件吗?
【发布时间】:2017-03-15 11:43:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: angular angular2-animation


    【解决方案1】:

    当然可以。你可以在不同的文件中声明动画,然后在你需要的地方导入它

    动画.ts

    export const animation = trigger(...)
    

    组件.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 {}
    

    【讨论】:

    • 与通过函数传递的变量有很好的接触:)
    • 非常感谢!您做得很好!
    • 在模板布局之外?如果你有一个像 angular cli 一样的 html 文件怎么办?
    【解决方案2】:

    当然可以。例如,您可以创建一个 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]
    })
    

    【讨论】:

      【解决方案3】:

      您和先生们当然在他的一些 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 致敬。

      【讨论】:

      • 提供的链接不再有效。
      【解决方案4】:

      这是另一个在 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

      【讨论】:

      • 感谢您在不以(不可持续的)模板格式编码时指定如何执行此操作。 :)
      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2014-01-02
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      相关资源
      最近更新 更多