Skip to content

Router: canActivateChild can't find guard provided in own module #12275

Closed

Description

@pesimeao

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When trying to use a children guard (canActivateChild) in the same module where the guard is provided, the guard fails to be injected.

Error: No provider for ChildGuard!

If we provide the guard in a parent module, it works.
If we provide the a canActivate guard in the same module where it is used, it works.

Expected behavior

The guard should be injected and work like the canActivate guard.

Minimal reproduction of the problem with instructions

Demo can be found here: http://plnkr.co/edit/JeLBnEgkoaHh4wDe9MAd?p=preview http://plnkr.co/edit/vc35AC1UIbZtlp0IjekX?p=preview

  1. Click on "Go to route with child guard" button.
  2. See error in console: Error: No provider for ChildGuard!

What happens:

  • when click on the button, router navigates to /admin
  • AdminModule provides ChildGuard service
  • Default route in AdminModule (empty) uses ChildGuard canActivateChild: [ChildGuard]

What is the motivation / use case for changing the behavior?

I have to protect all the child routes using a guard that is only known by that module, loaded by lazy-mode.

Please tell us about your environment:

Windows 10 Enterprise
Chrome 53.0.2785.116 m (64-bit)
IIS

  • Angular version: 2.1.0

  • Angular version (router): 3.1.0

  • Browser: Chrome 53.0.2785.116 m (64-bit)

  • Language: TypeScript 2.1.0-dev.20160811

  • Node (for AoT issues): node --version = v5.9.1

Activity

  1. DzmitryShylovich commented on Oct 13, 2016

    @DzmitryShylovich
    Contributor

    Can u pls simplify plunkr and remove all unnecessary components/services?

  2. pesimeao commented on Oct 17, 2016

    @pesimeao
    Author

    @DzmitryShylovich The plunkr is a fork of the official demo with minimal changes (only 4 changes - see first message) in order to reproduce the problem

  3. vicb commented on Dec 1, 2016

    @vicb
    Contributor

    Can you please keep only what's needed to demonstrate the issue ?

  4. pesimeao commented on Jan 2, 2017

    @pesimeao
    Author

    Hi,

    Here's a new plnk that contains only the necessary information.
    http://plnkr.co/edit/vc35AC1UIbZtlp0IjekX?p=preview

    When you access to /admin (click on button "Go to route..."), there's an error in the console No provider for ChildGuard!.

    This guard is provided in the AdminModule and used only in AdminModule.

  5. pesimeao commented on Jan 2, 2017

    @pesimeao
    Author

    I've also updated the first post ;)

  6. marvinscharle commented on Jan 18, 2017

    @marvinscharle

    This also affects child's entryComponents. They cannot be used outside the lazy loaded module.

  7. DzmitryShylovich commented on Jan 20, 2017

    @DzmitryShylovich
    Contributor

    @marvinscharle pls provide a small demo on Plunkr

  8. marvinscharle commented on Jan 20, 2017

    @marvinscharle

    @DzmitryShylovich here you go: http://plnkr.co/edit/9euisYeSNbEFrfzOhhzf?p=preview

    I created a simple setup, similar to what we use internally today. Main component provides a method to render a Type. EntryComponent is declared as entryComponent in Page1Module. However after having loaded Page1Module, when trying to render EntryComponent via ComponentFactoryResolver, No component factory found for EntryComponent is thrown.

    Use-cases for this approach are:

    • Modals that shall be rendered in the root component
    • Notifications
    • etc.

    by the way: thank you for the linked fixes 👍🏻

  9. added a commit that references this issue on Jan 29, 2017
    579567c
  10. DzmitryShylovich commented on Jan 31, 2017

    @DzmitryShylovich
    Contributor

    @marvinscharle the original issue was fixed. can you pls check out your issue with entry components after the next release and file a new bug if it wasn't fixed?

  11. added a commit that references this issue on Aug 11, 2017
    11bae5a
  12. added a commit that references this issue on Aug 28, 2017
    ee42b78
  13. angular-automatic-lock-bot commented on Sep 10, 2019

    @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.

  14. locked and limited conversation to collaborators on Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions