Angular 4.3新路由器动画不起作用

时间:2017-07-24 18:50:16

标签: angular animation angular-animations

我对新的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]': '' }

如果有人发现解决方案,请发布信息,告诉我们如何让它更容易,并且只在一个地方而不是每个控制器创建动画。

1 个答案:

答案 0 :(得分: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())
    ])
  ])
]);