My problem is that I have several pages which are using routerTransition as it can be found in several places (with the use of query(...)
method). The problem is that I would like to have a different animation when my component leaves or enters on browser's back button press. To be more specific:
- On regular navigation my component should enter from right to left
- On backward navigation my component should enter from left to right
and vica-versa for leaving.
If I could somehow detect in a router event if it's a forward or backward navigation, then I could easily do this, but I couldn't find out how to detect this without adding further data to my routes.
Edit:
I tried to do something like @Vega mentioned, I paste a snippet about it.
Template:
<div class="root" [@routerTransition]="navigationState$ | async"
(@routerTransition.done)="onDone($event)">
<sdx-router-outlet [size]="currentSize"></sdx-router-outlet>
</div>
Code:
this.navigationState$ = this._actions$.ofType(ActionTypes.GO_BACK)
.map(() => {
return { isGoingBack: true };
})
.merge(this._actions$.ofType(ActionTypes.GO_FORWARD).map(() => { return { isGoingBack: false }; }))
.map((navigation: Navigation) => navigation.isGoingBack ? 'backward' : 'forward')
.merge(this._resetNavigationState);
public onDone(event) {
// This is just to reset to a state where I can trigger my animation again
this._resetNavigationState.next('initial');
}
The idea here was that I set the transition trigger to forward
when navigation forward and backward
when navigating backward. However, because (I assume at least that this is the cause) the backward setting basicly "comes" from the ng-location service the state will be setted before the router starts navigating, and there won't be any animation because the trigger was fired before attaching or detaching components by the router-outlet.
As you can see I am using ngrx, but I don't think that it is important. The two types basicly just two Observables. One of them if firing on back button press, like @Vega said, the other one I fire with a custom router (for now) where I override the navigateByUrl method and in it I just dispatch a Forward action, then call the super.
What I didn't mentioned is that I don't want to put the animation to my components that I load, because all of the animation is the same. And if I make it like a "default" animation, then I don't have to be afraid of forgetting to put onto some components.