0

我对新的 Angular 4.3 动画有疑问。我认为我按照以下方式定义了一切正常:https ://medium.com/@gerard.sans/angular-supercharge-your-router-transitions-using-new-animation-features-v4-3-3eb341ede6c8但我没有在补丁更改期间,此时没有任何动画。下面我展示我的动画文件:

我的动画:

import {trigger, stagger, animate, style, group, query, transition} from '@angular/animations';

export const routerTransition = trigger('routerTransition', [
  transition('* <=> *', [
    /* order */
    /* 1 */ query(':enter, :leave', style({ position: 'fixed', width:'100%' })
      , { optional: true }),
    /* 2 */ group([  // block executes in parallel
      query(':enter', [
        style({ transform: 'translateX(100%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(0%)' }))
      ], { optional: true }),
      query(':leave', [
        style({ transform: 'translateX(0%)' }),
        animate('0.5s ease-in-out', style({ transform: 'translateX(-100%)' }))
      ], { optional: true }),
    ])
  ])
]);

如果我将此动画更改为另一个,我会看到不同的动作,但我从来没有取得好的结果(全宽页面动画等)。如果我从动画代码中删除 {optional: true} 我也会得到这个错误:

core.es5.js:1020 ERROR Error: Unable to process animations due to the following failed trigger transitions
@routerTransition has failed due to:
- `query(":enter")` returned zero elements. (Use `query(":enter", { optional: true })` if you wish to allow this.)
- `query(":leave")` returned zero elements. (Use `query(":leave", { optional: true })` if you wish to allow this.)

编辑:

我找不到解决方案,所以我回到每个子控制器中的旧版本动画:

 animations: [slideInOutAnimation],
  host: { '[@slideInOutAnimation]': '' }

如果有人找到解决方案,请发布信息,我们如何使其更容易并仅在一个地方而不是每个控制器中创建动画。

4

1 回答 1

0

我在 AOT 编译中出现了同样的错误。我的解决方案:

import {animate, animateChild, group, query as q, sequence, state, style, transition, trigger} from '@angular/animations';

// this solves them problem
export function query(s, a) {
    return q(s, a, {optional: true});
}

export const routerTransition = trigger('routerTransition', [
  transition('* => *', [
    query(':enter, :leave', style({position: 'fixed', width: '100%'})),
    query(':enter', style({transform: 'translateX(100%)'})),
    sequence([
      query(':leave', animateChild()),
      group([
        query(':leave', [
          style({transform: 'translateX(0%)'}),
          animate('400ms cubic-bezier(.75,-0.48,.26,1.52)',
            style({transform: 'translateX(-100%)'}))
        ]),
        query(':enter', [
          style({transform: 'translateX(100%)'}),
          animate('400ms cubic-bezier(.75,-0.48,.26,1.52)',
            style({transform: 'translateX(0%)'}))
        ])
      ]),
      query(':enter', animateChild())
    ])
  ])
]);
于 2017-08-09T15:14:50.437 回答