【问题标题】:How to get Angular Router Animations working with 'ng build --prod'?如何让 Angular 路由器动画与“ng build --prod”一起使用?
【发布时间】:2019-09-09 16:34:22
【问题描述】:

当我想使用 'ng build --prod' 构建我的 Angular 应用程序时出现错误:

src/app/route-animations.ts(15,9) 中的错误:“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(20,15):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(24,39):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(27,39):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(15,9):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(20,15):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(24,39):“slideTo”模板编译期间出错 不支持表达式形式。 src/app/route-animations.ts(27,39):“slideTo”模板编译期间出错 不支持表达式形式。

我真的不知道如何解决这个问题。

我在我的应用中添加了路由器动画,如下所示:

<div [@routeAnimations]="prepareRoute(outlet)" style="height: 100%">
  <router-outlet #outlet="outlet"></router-outlet>
</div>
import { slider } from './route-animations';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [ 
    slider
  ]
})
export class AppComponent implements OnInit {
  ngOnInit(): void {
    ...
  }

  prepareRoute(outlet: RouterOutlet) {
    return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
  }
}
import { trigger, transition, query, style, animate, group } from '@angular/animations';

export const slider =
  trigger('routeAnimations', [
    transition('isRight => isLeft', slideTo('left') ),
    transition('isLeft => isRight', slideTo('right') )
  ]);

export function slideTo(direction: string) {
  return [
    query(':enter, :leave', [
      style({
        position: 'absolute',
        top: 0,
        [direction]: 0,
        width: '100%'
      })
    ], { optional: true }),
    query(':enter', [
      style({ [direction]: '-100%'})
    ]),
    group([
      query(':leave', [
        animate('600ms ease', style({ [direction]: '100%'}))
      ], { optional: true }),
      query(':enter', [
        animate('600ms ease', style({ [direction]: '0%'}))
      ])
    ]),
  ];
}

【问题讨论】:

    标签: angular angular-router angular-animations angular-transitions


    【解决方案1】:

    你想要的不是完全可能的,因为动画不是一个更像装饰器模式语法的函数,但可能有一种方法可以重用某些动画,但关于此的文章非常有限。

    使用可从每个过渡选项传递的插值变量创建可重复使用的动画

    import { trigger, transition, query, style, animate, group, animation, useAnimation } from '@angular/animations';
    
    // Reusable animation
    export const slideAnimation = animation([
      query(':enter, :leave', [
        style({
          position: 'absolute',
          top: 0,
          left: '{{left}}',
          width: '100%'
        })
      ], { optional: true }),
      query(':enter', [
        style({ left: '-{{left}}' })
      ]),
      group([
        query(':leave', [
          animate('600ms ease', style({ left: '{{left}}' }))
        ], { optional: true }),
        query(':enter', [
          animate('600ms ease', style({ left: '{{left}}' }))
        ])
      ]),
    ]);
    
    
    export const slider = trigger('routeAnimations', [
      transition('isRight => isLeft', [useAnimation(slideAnimation, { params: { left: '0%', time: '2s' } })]),
      transition('isLeft => isRight', [useAnimation(slideAnimation, { params: { direction: '100%', time: '2s' } })])
    ]);
    

    发布的解决方案与您目前所拥有的不完全一样,这指向了正确的方向,我希望您可以让它发挥作用

    参考:https://angular.io/guide/reusable-animations

    【讨论】:

    • 感谢您的回答,但对我不起作用:src/app/route-animations.ts(3,17) 中的错误:不支持“ɵ0”函数表达式的模板编译期间出错在装饰器中考虑将函数表达式更改为导出函数。 src/app/route-animations.ts(3,17): 'ɵ0' 模板编译期间出错 装饰器不支持函数表达式 考虑将函数表达式更改为导出函数。
    • 出现另一个错误: ERROR in src/app/route-animations.ts(3,10): Error during template compile of 'slider' Reference to an non-exported function。 src/app/route-animations.ts(3,10): 'slider' 模板编译期间出错 引用非导出函数。
    • 给我一点时间让我检查一下
    • 检查这个 sn-p,我认为我们不能在 NgModule 装饰器声明的动画中使用标准函数,但检查它对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2020-01-13
    • 2018-08-09
    相关资源
    最近更新 更多