angular 2 route to 404 page when route param is invalid angular 2 route to 404 page when route param is invalid angular angular

angular 2 route to 404 page when route param is invalid


Update

In the new Router V3 you can use guards as explained in https://angular.io/guide/router#canactivate-requiring-authentication

Original

I think you should use @CanActivate() to do the check. If you forward in @CanActivate() the invalid URL shouldn't be added to the history (not tried)

See also https://github.com/angular/angular/issues/4112 for how to use DI in @CanActivate()


Ok, this is already implemented, just not well-documented.

According to the docs:
router.navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>

Has a parameter _skipLocationChange that will not modify the history.
These will do the trick:

router.navigateByUrl('/404', true);router.navigateByInstruction(router.generate(['/404']), true);


As of Angular 2 final this is the solution:

this._router.navigateByUrl('/404', { skipLocationChange: true })