Skip to content

Provide an official way to access the module/injector for a route #25630

@jonrimmer

Description

@jonrimmer

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

When lazy-loading modules, the router faces the problem of how to inject providers that are registered in the child module from a root service, since providers in lazy-loaded modules are not added to the root injector.

The implementation solves this problem by keeping a reference to the lazy-loaded module in the router config, which it can use to lookup the child module's injector. However, this capability is kept internal to the router, which prevents apps that have the same requirement from using it.

Now, consider an app that includes its own DI tokens on lazy-loaded route definitions. E.g.:

{
  path: '/project/:projectId',
  component: ProjectComponent
  data: {
    breadcrumb: ProjectBreadcrumbProvider
  }
}

If ProjectBreadcrumbProvider is registered in the lazy-loaded module, then a global BreadcrumbService in the root module cannot inject it using the root injector. It needs the injector associated with the current route instead, but there is no official way to get hold of it.

Expected behavior

The Router should provide an official way to access the module/injector for a given ActivatedRoute. I would suggest running the same closestLoadedConfig() logic eagerly and including the resulting module as a module property on each ActivatedRouteSnapshot instance.

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

Apps currently have two options:

  • Replicate the logic from closestLoadedConfig() and rely on the internal _loadedConfig property. But relying on private APIs is not a good idea, as these could change or be removed in a later version.
  • Register these providers in the root scope, rather than the lazy module. But this bloats the root module and defeats the purpose of lazy-loading.

Neither of these is preferable. An official way to get the module/injector for a given route would solve the problem.

Environment


Angular version: 6.1.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions