Skip to content

canMatch check throws NullInjectorError for a guard provided at the route level #46386

Description

@markostanimirovic

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

Activity

  1. changed the title [-]`canMatch` check throws `NullInjectorError` for a provider defined at the route level[/-] [+]`canMatch` check throws `NullInjectorError` for a guard provided at the route level[/+] on Jun 16, 2022
  2. JoostK commented on Jun 16, 2022

    @JoostK
    Member

    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.

  3. added this to the needsTriage milestone on Jun 16, 2022
  4. atscott commented on Jun 16, 2022

    @atscott
    Contributor

    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.

    The PR for CanMatch was started before the providers were added to the Route. This should actually work but I performed the rebase incorrectly. We can create the injector after a route matches via the path or match properties and before running the canMatch guards. This should still work as expected with respect to #45988.

  5. added a commit that references this issue on Jun 16, 2022
    82dcf54
  6. added
    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
    on Jun 16, 2022
  7. modified the milestones: needsTriage, Backlog on Jun 16, 2022
  8. angular-automatic-lock-bot commented on Jul 18, 2022

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Jul 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: routerstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions