angularangular-animationsangular2-aot

Angular 8 BrowserAnimations error with AOT


I have route-animations.ts that has some animations. when i run the code with ng serve it works fine but i get an error when running with ng serve --prod.


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

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

i get this error ERROR in src\app\app.component.ts(14,16): Error during template compile of 'AppComponent' Reference to a non-exported function in 'slider' 'slider' contains the error at src\app\animations\route-animations.ts(41,10).

import { Component } from '@angular/core';
import { MatIconRegistry } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';
import { RouterOutlet } from '@angular/router';
import { slider } from './animations/route-animations';;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
  animations: [slider]
})
export class AppComponent {
  constructor() { }

  prepareRoute(outlet: RouterOutlet) {
    return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
  }


}

Solution

  • The solution was quite simple, we just have to combine the exported const and function into one const.

    export const slider =
    trigger('routeAnimations', [
      transition('* => isLeft', [
        query(':enter, :leave', [
          style({
            position: 'absolute',
            top: 0,
            left: 0,
            width: '100%'
          })
        ], { optional: true }),
        query(':enter', [
          style({ left: '-100%'})
        ]),
        group([
          query(':leave', [
            animate('600ms ease', style({ left: '100%'}))
          ], { optional: true }),
          query(':enter', [
            animate('600ms ease', style({ left: '0%'}))
          ])
        ]),
      ]),
      transition('* => isRight', [
        query(':enter, :leave', [
          style({
            position: 'absolute',
            top: 0,
            right: 0,
            width: '100%'
          })
        ], { optional: true }),
        query(':enter', [
          style({ right: '-100%'})
        ]),
        group([
          query(':leave', [
            animate('600ms ease', style({ right: '100%'}))
          ], { optional: true }),
          query(':enter', [
            animate('600ms ease', style({ right: '0%'}))
          ])
        ]),
      ]),
      transition('isRight => *', [
        query(':enter, :leave', [
          style({
            position: 'absolute',
            top: 0,
            right: 0,
            width: '100%'
          })
        ], { optional: true }),
        query(':enter', [
          style({ left: '-100%'})
        ]),
        group([
          query(':leave', [
            animate('600ms ease', style({ left: '100%'}))
          ], { optional: true }),
          query(':enter', [
            animate('600ms ease', style({ left: '0%'}))
          ])
        ]),
      ]),
      transition('isLeft => *', [
        query(':enter, :leave', [
          style({
            position: 'absolute',
            top: 0,
            right: 0,
            width: '100%'
          })
        ], { optional: true }),
        query(':enter', [
          style({ right: '-100%'})
        ]),
        group([
          query(':leave', [
            animate('600ms ease', style({ right: '100%'}))
          ], { optional: true }),
          query(':enter', [
            animate('600ms ease', style({ right: '0%'}))
          ])
        ]),
      ])
    ],
    )```