Displaying a separate 404 not found page inside of a module in Angular
Clash Royale CLAN TAG#URR8PPP
Displaying a separate 404 not found page inside of a module in Angular
In my Angular application, I would like to have two separate Page Not Found pages.
In the app root, there is one in case the user goes something like: http://localhost:4200/xyz. This is fine.
However, I added one to my admin module which has an AuthGuard on it. If the user is logged in and navigates to http://localhost:4200/admin/xyz I would like them to go to my not found page within the admin module. For some reason they are getting logged out and redirected back to the Login page.
Please refer to the sample project below (attempt to go to /admin, login, then change the path to /admin/xyz and hit enter):
https://stackblitz.com/edit/angular-7tjyqx
Also, same thing happens with crisis-center which is not a protected module. Try going to /crisis-center/xyz and you will get redirected back to crisis-center instead of crisis - page not found
1 Answer
1
That is because services in angular (AuthService) are not shared as singletons for lazy-loaded modules. so every time you go to http://localhost:4200/admin/xyz directly from the address bar, a new service instance is created which sets isLogged to false and you are thrown to the Login page. However, if you log in and visit through
routerLink to http://localhost:4200/admin/xyz, you will get the no page
found and that is because you are inside same service instance of the module.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Excellent answer. So I can either stop lazy loading the modules so the entire app reloads each time or not be able to do it. Is it common to have a not found page within each separate module (feature of the system)? It seems like most people have one on the outer level, but the problem with that is my features have different looks and feels. So if a user is in the Admin feature I want the page not found to be inside that template, if the user is in another feature I want the page not found to be inside of that template. So is it basically not possible unless I stop lazy loading?
– Blake Rivell
Aug 11 at 19:37