Skip to content

ENVIRONMENT_INITIALIZER is eagerly loaded #45988

Closed

Description

@ArielGueta

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When using the ENVIRONMENT_INITIALIZER token in a lazy loaded component, the expected behaviour is that it should run the provider only when the path matches.

    {
    path: 'posts',
    loadComponent: () => import('./posts.component').then(m => m.PostsComponent),
    children: [
      {
        path: ':id',
        loadComponent: () => import('./post.component').then(m => m.PostComponent),
        providers: [{
          provide: ENVIRONMENT_INITIALIZER,
          multi: true,
          useValue() {
            console.log('loaded')
          }
        }]
      }
    ]

Why in this case, I see the log when navigate to posts. I should see it only when navigating to posts/:id.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

14.0.0-next.16

Anything else?

No response

Activity

  1. modified the milestone: needsTriage on May 13, 2022
  2. atscott commented on May 13, 2022

    @atscott
    Contributor

    Right, so the current implementation creates the EnvironmentInjector as soon as the route attempts a match. There's no technical reason we can't wait until after a match is confirmed. I'll work on making this change today.

    The reason ':id' injector is created even when just navigating to posts is that the children as still checked since they might contain an empty path.

  3. self-assigned this
    on May 13, 2022
  4. added a commit that references this issue on May 13, 2022
    77c9508
  5. ArielGueta commented on May 14, 2022

    @ArielGueta
    Author

    Thanks for the quick fix.

  6. angular-automatic-lock-bot commented on Jun 14, 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.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions