angular.io icon indicating copy to clipboard operation
angular.io copied to clipboard

To improve the router guide

Open olegdunkan opened this issue 8 years ago • 6 comments
trafficstars

angular/angular#12995 Resolver can inject custom providers from application root only.

olegdunkan avatar Feb 03 '17 07:02 olegdunkan

This is not true and is already stated in the docs

Guards and the service providers they require must be provided at the module-level. This allows the Router access to retrieve these services from the Injector during the navigation process. The same rule applies for feature modules loaded asynchronously.

brandonroberts avatar Feb 06 '17 01:02 brandonroberts

@brandonroberts What is not true? Module-level providers are gathered in application root injector. Every resolver that router instantiates along the path inherits from application root or how you said module-level. We were talking in (angular/angular#12995) about case where I have supposed that resolver can inject services from the most closer component injector.

Let say we have this route:

Root(Service("Root")) -> Parent(Service("Parent"))->Child(Service)
                                                  ->Resolver(Service) 

In `Child` we have Service("Parent")
In 'Resolver' we have Service("Root")

Resolver's injector doesn't see Service("Parent") it sees only Service("Root")

olegdunkan avatar Feb 06 '17 13:02 olegdunkan

Module-level providers are gathered in the application root injector only if they're imported into the root module. The case is not the same for lazy loaded modules. Component injectors can't be used because all guards and resolvers are run before any components are rendered. This is why resolve and guard providers must be used at the module level so the module-level injector can retreive them.

brandonroberts avatar Feb 06 '17 14:02 brandonroberts

@brandonroberts yes, you try to explain me for the second time) why resolver can't inject service from closest component. @DzmitryShylovich ask me to create issue in this repo to express this case in documentation.

Guards and the service providers they require must be provided at the module-level. This allows the Router access to retrieve these services from the Injector during the navigation process. The same rule applies for feature modules loaded

This text is not about my case. You can't conclude from it from what places resolver can get services.

olegdunkan avatar Feb 06 '17 14:02 olegdunkan

I see. I'll add an explicit mention to the guide about the same requirement for resolvers .

brandonroberts avatar Feb 06 '17 14:02 brandonroberts

ok @brandonroberts

olegdunkan avatar Feb 06 '17 14:02 olegdunkan