5

I have followed the Tour-of-Hero tutorial on Angular2 official page. But I took it a step further by introducing Modules lazy-loading.

I've got the following Feature Modules:

  • Module Dashboard

    • dashboard component
  • Module Hero

    • hero-list component
    • hero-detail component
    • hero-search component
    • hero-search service
    • hero model

The hero-search component is a directive that should be used in the Dashboard Module.

However, according to this documentation about Angular2 architechture, Routed module should not be imported by others. Meaning, I should not import the Hero module into the Dashboard module. If I do, my routes are no longer working anyway.

Also the hero-search component uses a hero-service and the hero-model, so it has strong dependencies on Hero Module, making it hard to externalize.

So how am I supposed to use the hero-search component in the Dashboard component ?

2
  • Sam, have you already found a solution to this problem? I'm struggling with the same issue. Commented Sep 27, 2016 at 12:47
  • No, I have no idea what is the correct approach on this. I'm looking forward to John Papa's tutorial on ngModule. Commented Sep 27, 2016 at 14:23

2 Answers 2

2

You can make a third module, let's call it UtilityModule, export HeroSearch from there and then import UtilityModule to DashboardModule and HeroModule. That way, you will be able to use HeroSearch component in both modules.

@NgModule({
  imports: [ ... ],
  declarations: [ ... ],
  exports: [ HeroSearch ]
})

export class UtilityModule { }
Sign up to request clarification or add additional context in comments.

3 Comments

Stefan, I've thought of doing that, but the HeroSearchComponent has strong dependencies on a HeroSearchService and on the Hero model. I've update my post accordingly. This makes it hard to put in a SharedModule (or UtiltyModule)
@Sam Why don't you also move all needed dependencies for HeroSearch to UtilityModule as well?
Well, simply because the Hero Model or the Hero Service should really be in the Hero Module, not in a Shared Module. I'm surprised this issue was not addressed by the Angular Team in the tutorial as it is a fundamental question in my opinion. I can see many use-cases where I would need to import only one specific component of a lazy-loaded module into another module.
0

As I know you can't do that. Lazy routed modules acts like an independent pieces of your app. So anything declared there should be used only inside that module's scope. Think about it. When you load your Dashboard you can never be sure about your lazy Hero module was loaded yet at all. So how you would like to use anything from a module which maybe wasn't loaded yet?

Maybe I'am wrong but I also try to design a more complex module structure than they shown us in the docs without success. I really think that this module system is an example of a bad rush design. Good luck with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.