I get this error Error: NG04014: Invalid configuration of route ‘inspect//’: redirectTo and canActivate cannot be used together. Redirects happen before activation so canActivate will never be executed. after upgrading angular 13 to 14. Anyone can help ?
I think it’s related to validating the router lazy loaded configs, which was listed in the Angular 14 Router breaking changes:
Lazy loaded configs are now also validated once loaded like the initial set of routes are. Lazy loaded modules which have invalid Route configs will now error. Note that this is only done in dev mode so there is no production impact of this change.
I think you have two options here to handle it correctly:
- Handle the redirect in the guard by returning a UrlTree:
If any guard returns a UrlTree, the current navigation is cancelled and a new navigation begins to the UrlTree returned from the guard. https://angular.io/api/router/CanActivate
- Move/Apply the
canActivateguard on the route specified in the
redirectTo, without applying it on the same route.
Answered by Amer
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.