question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hierarchical injector

See original GitHub issue

Hi, in order to make more reusable code, I would like to create a module which use a component which is not a part of this module, nor include in an imported module. The component will be inject by the ApplicationModule (main module). It seems that this is not supported?

Example: AuthenticationModule has different controllers which use a UserService dependency. UserModule also has different controllers which use a UserService dependency.

Depending on application, the UserService may be implemented in different way. What I want is defining this in the ApplicationModule for all modules, once for all.

@Module({
    modules: [ AuthenticationModule, UserModule ]
    components: [
        { provide: UserService, useClass: MyCustomUserService }
    ]
})
class ApplicationModule {}

@Module({
    controllers: [],
    components: [ AuthenticationService ],
    exports: [ 
        AuthenticationService
    ]
})
class AuthenticationModule {}

export class AuthenticationService {
    constructor(private userService: UserService) {}
}

I was expecting below code to work (similar to angular), but the userService dependency is not found.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
kamilmysliwieccommented, May 14, 2017

Hi @AurelieV, I made a set of improvements for a dependency injector, so now your example should works fine. Please, update your packages (since ~2.* version @nestjs/core, @nestjs/common etc.) into latest versions.

2reactions
AurelieVcommented, May 12, 2017

The problem is I do not want my two modules being dependant on each others. AuthenticationService can be use without UserModule. UserService is not part of UserModule

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hierarchical injectors - Angular
With hierarchical dependency injection, you can isolate sections of the application and give them their own private dependencies not shared with the rest...
Read more >
Hierarchical dependency injectors - Angular - w3resource
The Angular dependency injection system is hierarchical. There is a tree of injectors that parallels an app's component tree.
Read more >
Understand Angulars Hierarchical Dependency Injection system
A hierarchical dependency injection system allows us to define different boundaries or scopes for our dependencies to run in and follows the ...
Read more >
Angular Dependency Injection – Understanding hierarchical ...
Learn how to build custom form controls like select dropdown with multiselection in my new advanced Angular Forms course ...
Read more >
Hierarchical injectors | Semantic portal — learn smart!
Injectors in Angular have rules that you can leverage to achieve the desired visibility of injectables in your apps. By understanding these rules, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found