1

I have been trying to get angular animations to work with routes,so angular route transition animations. When using transition('* => *')... the code executes, but while trying to get 'void => *' or vice versa, the code does not work at all. No animations take place. Please Help!

My app.component.ts is:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  animations: [getRoutingTrigger()]
})
export class AppComponent {
  title = 'screen-transition';
  in = "in";
  getRoutingState(outlet: RouterOutlet): string {
    console.log( outlet.activatedRouteData.state);
    return outlet.activatedRouteData.state;
  }
}

My html file is:

<div class = "main">
    <a class="btn btn-info my-1" routerLink="/routeToComponent">Route To Component</a>
    <a class="btn btn-info my-1" routerLink="/secondRoute">Second Route</a>
    <a class="btn btn-info my-1" routerLink="/thirdRoute">Third Route</a>
    <a class ="btn btn-info my-1" routerLink="/firstRoute">FirstRoute</a>

    <div [@routing]="getRoutingState(outlet)" class="main-container">
        <router-outlet #outlet="outlet"></router-outlet>
    </div>
    <div style="background-color: brown" class="my-3">
    </div>
</div>

My router.module.ts is

const routes: Routes = [
    {path: 'routeToComponent' , component: RouteToComponent, data: {state: 'routeToComponent'}},
    {path: 'secondRoute', component: SecondRouteComponent, data: {state: 'secondRoute'}},
    {path: 'thirdRoute', component: ThirdRouteComponent, data: {state: 'thirdRoute'}},
    {path: 'firstRoute', component: FirstComponent, data: {state: 'firstRoute'}}
  ];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule{}

And my animations file is:

import {
trigger,
animate,
transition,
style,
query,
group,
useAnimation,
animation,
AnimationTriggerMetadata,
state
} from '@angular/animations';

const commonStyles = {
  position: 'absolute',
  width: '100%'
};
const slideBackwards = animation([
  query(':leave', [
      style([commonStyles, { transform: 'translateX(0%)' }])
  ], { optional: true }),
  query(':enter', [
      style([commonStyles, { transform: 'translateX(-200%)' }])
  ], { optional: true }),
  group([
      query(':leave', [
          animate('1000ms ease-out',
              style({ transform: 'translateX(200%)' })
          )
      ], { optional: true }),
      query(':enter', [
          animate('1000ms ease-out',
              style({ transform: 'translateX(0%)' }))
      ], { optional: true })
  ])
]);

const slideForward = animation([
  query(':leave', [
      style([commonStyles, { transform: 'translateX(0%)' }])
  ], { optional: true }),
  query(':enter', [
      style([commonStyles, { transform: 'translateX(200%)' }])
  ], { optional: true }),
  group([
      query(':leave', [
          animate('1000ms ease-in-out',
              style({ transform: 'translateX(-200%)' })
          )
      ], { optional: true }),
      query(':enter', [
          animate('1000ms ease-in-out',
              style({ transform: 'translateX(0%)' }))
      ], { optional: true })
  ])
]);

export function getRoutingTrigger(): AnimationTriggerMetadata {

  return trigger('routing', [
    transition("void => *", [useAnimation(slideForward)]),//doesnt work

    transition('* => void', [useAnimation(slideBackwards)]),//doesnt work

    transition('* => *', [useAnimation(slideBackwards)])//only the one that works.
    //if i remove the last one nothing happens 

  ]);
}
Augustin R
  • 7,089
  • 3
  • 26
  • 54
  • I just put an answer to a similar question: https://stackoverflow.com/a/51783181/8945135 – ibenjelloun Aug 10 '18 at 09:28
  • @ibenjelloun thanks for the response. The example you demonstrate is based on child components whereas my components are stand alone. The issue is why the **void** state would not work even though the angular should have void state in built. My example works perfectly with me specifying the routes and with `*=>*` but it brings the problem of having the animations in only one direction. Thanks – hemang sharma Aug 10 '18 at 13:11

0 Answers0