Let's say we have the following route configuration:
[{ path: "people", children: [ { path: ":id", component: PersonProfileComponent, resolve: { person: PersonResolver }, children: [ { path: "", component: PersonOverviewComponent }, { path: "photos", component: PersonPhotoListComponent } ] } ] }, { path: "404", component: PageNotFoundComponent }, { path: "**", component: PageNotFoundComponent }]
I want to redirect to page 404 when the user tries to go to any of them:
/people/{non-existing-id} /people/{non-existing-id}/photos /people/{existing-id}/{non-existing-subpage}
The third case is handled by the ** route, since it does not match any of the previous routes. Thus, PersonResolver must handle the first two cases.
export class PersonResolver implements Resolve<Person> { constructor(private peopleService: PeopleService, private router: Router) { } resolve(route: ActivatedRouteSnapshot): Observable<Person> { return this.peopleService.find(id).catch(error => { if (error instanceof Response) { if (error.status === 404) { this.router.navigate(["/404"]); } else {
Everything works, but the problem is that when the 404 page is displayed, the browser URL is updated to <hostname>/404 (as expected). My question is how to set the URL of the path just canceled (e.g. <hostname>/people/fake-id/photos ), since this 404 is handled all over the Internet. (I also tried passing {skipLocationChange: true} to router.navigate() , but this saves space before we try to jump)
source share