Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 route async resolve does not keep the location on navigate

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);
    });
} 
like image 719
izupet Avatar asked Dec 18 '16 15:12

izupet


1 Answers

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.

like image 171
André Avatar answered Oct 20 '22 06:10

André