Repository navigation
canMatch check throws NullInjectorError for a guard provided at the route level #46386
Copy link
Copy link
Closed
Closed
Copy link
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentAn issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: routerstate: has PR
Milestone
Description
Which @angular/* package(s) are the source of the bug?
router
Is this a regression?
No
Description
There is the NullInjectorError if we try to use the CanMatch guard that is provided at the route level:
export class Guard implements CanMatch {
canMatch(): boolean {
return true;
}
}
export const appRoutes: Route[] = [
{
path: 'foo',
component: FooComponent,
// error
canMatch: [Guard],
providers: [Guard],
},
];If we try to use e.g. CanActivate guard that is provided at the route level, it will be properly injected:
export const appRoutes: Route[] = [
{
path: 'foo',
component: FooComponent,
// works
canActivate: [Guard],
providers: [Guard],
},
];Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[Guard -> Guard -> Guard]:
NullInjectorError: No provider for Guard!
NullInjectorError: R3InjectorError(AppModule)[Guard -> Guard -> Guard]:
NullInjectorError: No provider for Guard!
at NullInjector.get (core.mjs:9091:27)
at R3Injector.get (core.mjs:9258:33)
at R3Injector.get (core.mjs:9258:33)
at R3Injector.get (core.mjs:9258:33)
at NgModuleRef.get (core.mjs:22413:33)
at router.mjs:3313:32
at Array.map (<anonymous>)
at runCanMatchGuards (router.mjs:3312:42)
at matchWithChecks (router.mjs:3340:12)
at ApplyRedirects.matchSegmentAgainstRoute (router.mjs:3674:16)
at resolvePromise (zone.js:1211:31)
at resolvePromise (zone.js:1165:17)
at zone.js:1278:17
at _ZoneDelegate.invokeTask (zone.js:406:31)
at Object.onInvokeTask (core.mjs:26501:33)
at _ZoneDelegate.invokeTask (zone.js:405:60)
at Zone.runTask (zone.js:178:47)
at drainMicroTaskQueue (zone.js:585:35)
at ZoneTask.invokeTask [as invoke] (zone.js:491:21)
at invokeTask (zone.js:1648:18)
Please provide the environment you discovered this bug in (run ng version)
Angular CLI: 14.0.1
Node: 14.17.1
Package Manager: yarn 1.22.17
OS: win32 x64
Angular: 14.0.1
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1400.1
@angular-devkit/build-angular 14.0.1
@angular-devkit/core 14.0.1
@angular-devkit/schematics 14.0.1
@angular/router 14.1.0-next.1+sha-df1652e
@schematics/angular 14.0.1
rxjs 7.5.5
typescript 4.6.4
Anything else?
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentAn issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: routerstate: has PR
Activity
[-]`canMatch` check throws `NullInjectorError` for a provider defined at the route level[/-][+]`canMatch` check throws `NullInjectorError` for a guard provided at the route level[/+]JoostK commentedon Jun 16, 2022
This is a side-effect of #45990 to address #45988. This would likely need to be addressed through a documentation update, as your expectation is incompatible with the expectation that was raised in #45988.
atscott commentedon Jun 16, 2022
The PR for
CanMatchwas started before theproviderswere added to theRoute. This should actually work but I performed the rebase incorrectly. We can create the injector after a route matches via thepathormatchproperties and before running thecanMatchguards. This should still work as expected with respect to #45988.fix(router): Ensure Route injector is created before running CanMatch…
angular-automatic-lock-bot commentedon Jul 18, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.