title: Route animation angular 4
body: <p>The app I am working on requires the ability to navigate back and forth through the routes so when you navigate one way through the app in one direction the components animate from left to right, and when you navigate back they navigate from right to left.</p>
  
  <p>i.e. </p>
  
  <pre><code>component1 -&gt; component2 =  left -&gt; right animation
  component2 -&gt; component1 = right -&gt; left animation
  </code></pre>
  
  <p>I can achieve this easily enough in NG4 but the issue is that it doesn't navigate both the exit and entering components. The exiting component just disappears and the entering component animates in.</p>
  
  <p>I can also achieve this in NG2 - but the complication arises when you have conditional animation. ie.</p>
  
  <pre><code>component2 -&gt; component3 = left -&gt; right animation
  component2 -&gt; component1 = right -&gt; left animation
  </code></pre>
  
  <p>The problem here is triggering a different animation depending on the next or previous route.</p>
  
  <p><strong>Does anyone have a solution in either NG2 or 4 to solve the issue of animating both views simultaneously in either direction?</strong></p>
  
  <p>I haven't put this in plunkr because I have no idea how to set up an NG4 app in that. Apologies.</p>
  
  <p>My NG4 solution is based on <a href="https://github.com/matsko/ng4-animations-preview" rel="noreferrer">this demo app</a> <a href="https://github.com/matsko/ng4-animations-preview" rel="noreferrer">https://github.com/matsko/ng4-animations-preview</a></p>
  
  <p>but essentially in my app.component.ts I have the following animation:</p>
  
  <pre><code>animations: [
      trigger('routerAnimations',[
          transition('page1 =&gt; page2',[
              style({transform: 'translateX(100%)'}),
              animate('500ms')
          ]),
          transition('page2 =&gt; page1', [
              style({transform: 'translateX(-100%)'}),
              animate('500ms')
          ]),
          transition('page2 =&gt; page3',[
              style({transform: 'translateX(100%)'}),
              animate('500ms')
          ]),
          transition('page3 =&gt; page2',[
              style({transform: 'translateX(-100%)'}),
              animate('500ms')
          ]),
          transition('page3 =&gt; page4',[
              style({transform: 'translateX(100%)'}),
              animate('500ms')
          ]),
          transition('page4 =&gt; page3',[
              style({transform: 'translateX(-100%)'}),
              animate('500ms')
          ])
      ])
  ]
  </code></pre>
  
  <p>and a function that grabs an animation value from each route and binds (?) it to the outlet variable on the <code>&lt;router-outlet&gt; in app.component.html</code>: </p>
  
  <pre><code>export class AppComponent {
      prepareRouteTransition(outlet) {
          let routeData: any;
          try {
              routeData = outlet['_activatedRoute'].snapshot.routeConfig['animation'];
         } catch(e) {
             return '';
         }
         return routeData.value;
      }
  }
  </code></pre>
  
  <p>Function above grabs the animation value which is used in the animation to define the transition start and end values:</p>
  
  <pre><code>export const APP_ROUTES = [{
      path: '',
      component: Page1Component,
      animation: {
        value: 'page1',
      }
    }, 
    {
      path: 'page2',
      component: Page2Component,
      animation: {
        value: 'page2',
      }
    },
    {
      path: 'page3',
      component: Page3Component,
      animation: {
          value: 'page3'
      }
    },
    {
        path: 'page4',
        component: Page4Component,
        animation: {
            value: 'page4'
        }
    }
  ];
  </code></pre>
  
  <p>app.component.html:</p>
  
  <pre><code>&lt;div class="page" [@routerAnimations]="prepareRouteTransition(outlet)"&gt;
    &lt;router-outlet #outlet="outlet"&gt;&lt;/router-outlet&gt;
  &lt;/div&gt;
  </code></pre>