15

I try to navigate user to error page when trying to access not allowed page. The problem is that the skipLocationChange does not work in this occasion. It navigates to error page but the url changes to the root. How to keep the original url user provided?

resolve(route: ActivatedRouteSnapshot): Observable<any|boolean>|boolean {
    return this.apiclientService.get('cars/' + route.params['id']).map(
        response => {
            if (response.data.user_id === this.authService.user().id) {
                return response.data;
            }

            this.router.navigate(['/404'], { skipLocationChange: true });
            return false;
        }
    ).catch(error => {
        this.router.navigate(['/404'], { skipLocationChange: true });

        return Observable.of(false);
    });
} 
izupet
  • 1,529
  • 5
  • 20
  • 42
  • Why aren't you using guards? https://angular.io/docs/ts/latest/guide/router.html#!#guards Not sure if this fixes your problem though but I think that's exactly what guards are for. – Günter Zöchbauer Dec 21 '16 at 06:27
  • I tried guard (canActivate) but the issue is the same + can't pass data to controller as we can with resolve. If you have any example I'll appreciate. – izupet Dec 21 '16 at 07:51
  • Sorry, somehow I assumed your code is in a component. `resolve` is what I meant anyway. A Plunker that allows to reproduce would be great also to see the routes. – Günter Zöchbauer Dec 21 '16 at 07:52
  • @izupet, How did you solve it? – securecurve Dec 09 '17 at 06:47

2 Answers2

1

I think skipLocationChange is actually working. The thing is Angular did not navigate to the new route because the guard failed. If you want to capture the failing URL, inspect the route: ActivatedRouteSnapshot param.

André
  • 12,497
  • 6
  • 42
  • 44
0

The issue is that this.route is injected at the root level so it will be the root route.

You can traverse the routes to find the one you are intersted in.

TargunTech
  • 1,132
  • 11
  • 19