In AngularJs with UI-Router, I could do the following to have any unauthorized or non-existing state to be shown a particular view without need of redirecting so the user could know what route/url was the unsuccessful one:
.state('notauthorized', {
templateUrl: 'notauthorized.html'
})
.state('notfound', {
templateUrl: 'notfound.html'
});
Now I'm working on Angular 4 with the built-in RouteModule. Is there any way to accomplish the same without redirecting the user to a completely different route??
I don't know if that's possible but you could always redirect to the 'not found' view, but tell the router to not change the URL so it appears to the user that they are still on the same page / URL.
use the skipLocationChange option from here https://angular.io/api/router/NavigationExtras
in the component:
router.navigateByUrl("/pathToYourNotFoundView", { skipLocationChange: true });
or if it was a link in the template -
<a [routerLink]="..." skipLocationChange>click me</a>