How to load a shared component lazily in Angular?










-1















I have a component CompA, which is used in 2 lazy-load modules, LazyModuleA and LazyModuleB. I know the way that creating a SharedModule and import it in both lazy-load modules works. But the SharedModule is not lazy-loaded and will be bundled in main.js.



Is there a way that make CompA lazy-loaded while can be shared between lazy-loaded modules?










share|improve this question






















  • why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

    – pixelbits
    Nov 14 '18 at 4:44











  • @pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

    – Liranius
    Nov 14 '18 at 5:51











  • Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

    – pixelbits
    Nov 14 '18 at 5:52















-1















I have a component CompA, which is used in 2 lazy-load modules, LazyModuleA and LazyModuleB. I know the way that creating a SharedModule and import it in both lazy-load modules works. But the SharedModule is not lazy-loaded and will be bundled in main.js.



Is there a way that make CompA lazy-loaded while can be shared between lazy-loaded modules?










share|improve this question






















  • why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

    – pixelbits
    Nov 14 '18 at 4:44











  • @pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

    – Liranius
    Nov 14 '18 at 5:51











  • Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

    – pixelbits
    Nov 14 '18 at 5:52













-1












-1








-1








I have a component CompA, which is used in 2 lazy-load modules, LazyModuleA and LazyModuleB. I know the way that creating a SharedModule and import it in both lazy-load modules works. But the SharedModule is not lazy-loaded and will be bundled in main.js.



Is there a way that make CompA lazy-loaded while can be shared between lazy-loaded modules?










share|improve this question














I have a component CompA, which is used in 2 lazy-load modules, LazyModuleA and LazyModuleB. I know the way that creating a SharedModule and import it in both lazy-load modules works. But the SharedModule is not lazy-loaded and will be bundled in main.js.



Is there a way that make CompA lazy-loaded while can be shared between lazy-loaded modules?







angular typescript lazy-loading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 2:57









LiraniusLiranius

636




636












  • why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

    – pixelbits
    Nov 14 '18 at 4:44











  • @pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

    – Liranius
    Nov 14 '18 at 5:51











  • Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

    – pixelbits
    Nov 14 '18 at 5:52

















  • why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

    – pixelbits
    Nov 14 '18 at 4:44











  • @pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

    – Liranius
    Nov 14 '18 at 5:51











  • Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

    – pixelbits
    Nov 14 '18 at 5:52
















why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

– pixelbits
Nov 14 '18 at 4:44





why would the sharedmodule (if its only imported by the lazy modules) be bundled with main?

– pixelbits
Nov 14 '18 at 4:44













@pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

– Liranius
Nov 14 '18 at 5:51





@pixelbits Well, I have a CoreModule which imported SharedModule to get access to some of the shared components. And obviously, the CoreModule is imported in AppModule.

– Liranius
Nov 14 '18 at 5:51













Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

– pixelbits
Nov 14 '18 at 5:52





Easy solution is split your shared module. One which is imported by AppModule, another that is imported by lazy feature modules.

– pixelbits
Nov 14 '18 at 5:52












1 Answer
1






active

oldest

votes


















0














I think you are confusing with shared module and lazy loading modules -



Lazy loading modules are called only on demand whereas shared module is totally different from it.



Let's come to shared module - In your App if you have around 3 modules and all the three modules relay on a same component directive or modules instead of importing all these in each and every modules you can just import in your shared module and map it in all your modules



AppModule(Root Module)



@NgModule(
declarations: ,
imports: [
BrowserModule,
HttpClientModule,
FormsModule,
],
providers: ,
bootstrap: [AppComponent]
)
export class AppModule


LazyModule



@NgModule(
imports: [
CommonModule,
HttpClientModule,
FormsModule,
BoatsManagerRoutingModule,
],
declarations: ,
providers:
)
export class LazyModule


Check the above modules - you might see HttpClientModule and FormsModule are commonly imported in both the modules now the act of Shared module comes into play



SharedModule



 @NgModule(
imports: [
CommonModule,
HttpClientModule,
FormsModule,
],
declarations: ,
providers: ,
exports: [
HttpClientModule,
FormsModule
]
)
export class SharedModule


Now I Have imported those two modules in a Shared module and then exported the same, like the same way if you have any components or directives common to both can be declared in the shared module and export the same - Now only one thing you want to do is that directly import the Shared module you have created above in both the AppModule and LazyModule then you will have access to both HttpClientModule and FormsModule



So that is use of SharedModule there is no need of lazy loading the shared module because it will get imported to the modules and get access to them



Finally loading loading Lazy modules you can use loadChildern property on your routes that will load the module Lazily



 
path: 'lazy',
loadChildren: './folder/Lazy.module#LazyModule',



Hope this solves your problem - Happy coding !!






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292542%2fhow-to-load-a-shared-component-lazily-in-angular%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I think you are confusing with shared module and lazy loading modules -



    Lazy loading modules are called only on demand whereas shared module is totally different from it.



    Let's come to shared module - In your App if you have around 3 modules and all the three modules relay on a same component directive or modules instead of importing all these in each and every modules you can just import in your shared module and map it in all your modules



    AppModule(Root Module)



    @NgModule(
    declarations: ,
    imports: [
    BrowserModule,
    HttpClientModule,
    FormsModule,
    ],
    providers: ,
    bootstrap: [AppComponent]
    )
    export class AppModule


    LazyModule



    @NgModule(
    imports: [
    CommonModule,
    HttpClientModule,
    FormsModule,
    BoatsManagerRoutingModule,
    ],
    declarations: ,
    providers:
    )
    export class LazyModule


    Check the above modules - you might see HttpClientModule and FormsModule are commonly imported in both the modules now the act of Shared module comes into play



    SharedModule



     @NgModule(
    imports: [
    CommonModule,
    HttpClientModule,
    FormsModule,
    ],
    declarations: ,
    providers: ,
    exports: [
    HttpClientModule,
    FormsModule
    ]
    )
    export class SharedModule


    Now I Have imported those two modules in a Shared module and then exported the same, like the same way if you have any components or directives common to both can be declared in the shared module and export the same - Now only one thing you want to do is that directly import the Shared module you have created above in both the AppModule and LazyModule then you will have access to both HttpClientModule and FormsModule



    So that is use of SharedModule there is no need of lazy loading the shared module because it will get imported to the modules and get access to them



    Finally loading loading Lazy modules you can use loadChildern property on your routes that will load the module Lazily



     
    path: 'lazy',
    loadChildren: './folder/Lazy.module#LazyModule',



    Hope this solves your problem - Happy coding !!






    share|improve this answer



























      0














      I think you are confusing with shared module and lazy loading modules -



      Lazy loading modules are called only on demand whereas shared module is totally different from it.



      Let's come to shared module - In your App if you have around 3 modules and all the three modules relay on a same component directive or modules instead of importing all these in each and every modules you can just import in your shared module and map it in all your modules



      AppModule(Root Module)



      @NgModule(
      declarations: ,
      imports: [
      BrowserModule,
      HttpClientModule,
      FormsModule,
      ],
      providers: ,
      bootstrap: [AppComponent]
      )
      export class AppModule


      LazyModule



      @NgModule(
      imports: [
      CommonModule,
      HttpClientModule,
      FormsModule,
      BoatsManagerRoutingModule,
      ],
      declarations: ,
      providers:
      )
      export class LazyModule


      Check the above modules - you might see HttpClientModule and FormsModule are commonly imported in both the modules now the act of Shared module comes into play



      SharedModule



       @NgModule(
      imports: [
      CommonModule,
      HttpClientModule,
      FormsModule,
      ],
      declarations: ,
      providers: ,
      exports: [
      HttpClientModule,
      FormsModule
      ]
      )
      export class SharedModule


      Now I Have imported those two modules in a Shared module and then exported the same, like the same way if you have any components or directives common to both can be declared in the shared module and export the same - Now only one thing you want to do is that directly import the Shared module you have created above in both the AppModule and LazyModule then you will have access to both HttpClientModule and FormsModule



      So that is use of SharedModule there is no need of lazy loading the shared module because it will get imported to the modules and get access to them



      Finally loading loading Lazy modules you can use loadChildern property on your routes that will load the module Lazily



       
      path: 'lazy',
      loadChildren: './folder/Lazy.module#LazyModule',



      Hope this solves your problem - Happy coding !!






      share|improve this answer

























        0












        0








        0







        I think you are confusing with shared module and lazy loading modules -



        Lazy loading modules are called only on demand whereas shared module is totally different from it.



        Let's come to shared module - In your App if you have around 3 modules and all the three modules relay on a same component directive or modules instead of importing all these in each and every modules you can just import in your shared module and map it in all your modules



        AppModule(Root Module)



        @NgModule(
        declarations: ,
        imports: [
        BrowserModule,
        HttpClientModule,
        FormsModule,
        ],
        providers: ,
        bootstrap: [AppComponent]
        )
        export class AppModule


        LazyModule



        @NgModule(
        imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        BoatsManagerRoutingModule,
        ],
        declarations: ,
        providers:
        )
        export class LazyModule


        Check the above modules - you might see HttpClientModule and FormsModule are commonly imported in both the modules now the act of Shared module comes into play



        SharedModule



         @NgModule(
        imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        ],
        declarations: ,
        providers: ,
        exports: [
        HttpClientModule,
        FormsModule
        ]
        )
        export class SharedModule


        Now I Have imported those two modules in a Shared module and then exported the same, like the same way if you have any components or directives common to both can be declared in the shared module and export the same - Now only one thing you want to do is that directly import the Shared module you have created above in both the AppModule and LazyModule then you will have access to both HttpClientModule and FormsModule



        So that is use of SharedModule there is no need of lazy loading the shared module because it will get imported to the modules and get access to them



        Finally loading loading Lazy modules you can use loadChildern property on your routes that will load the module Lazily



         
        path: 'lazy',
        loadChildren: './folder/Lazy.module#LazyModule',



        Hope this solves your problem - Happy coding !!






        share|improve this answer













        I think you are confusing with shared module and lazy loading modules -



        Lazy loading modules are called only on demand whereas shared module is totally different from it.



        Let's come to shared module - In your App if you have around 3 modules and all the three modules relay on a same component directive or modules instead of importing all these in each and every modules you can just import in your shared module and map it in all your modules



        AppModule(Root Module)



        @NgModule(
        declarations: ,
        imports: [
        BrowserModule,
        HttpClientModule,
        FormsModule,
        ],
        providers: ,
        bootstrap: [AppComponent]
        )
        export class AppModule


        LazyModule



        @NgModule(
        imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        BoatsManagerRoutingModule,
        ],
        declarations: ,
        providers:
        )
        export class LazyModule


        Check the above modules - you might see HttpClientModule and FormsModule are commonly imported in both the modules now the act of Shared module comes into play



        SharedModule



         @NgModule(
        imports: [
        CommonModule,
        HttpClientModule,
        FormsModule,
        ],
        declarations: ,
        providers: ,
        exports: [
        HttpClientModule,
        FormsModule
        ]
        )
        export class SharedModule


        Now I Have imported those two modules in a Shared module and then exported the same, like the same way if you have any components or directives common to both can be declared in the shared module and export the same - Now only one thing you want to do is that directly import the Shared module you have created above in both the AppModule and LazyModule then you will have access to both HttpClientModule and FormsModule



        So that is use of SharedModule there is no need of lazy loading the shared module because it will get imported to the modules and get access to them



        Finally loading loading Lazy modules you can use loadChildern property on your routes that will load the module Lazily



         
        path: 'lazy',
        loadChildren: './folder/Lazy.module#LazyModule',



        Hope this solves your problem - Happy coding !!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 7:05









        RahulRahul

        1,0992318




        1,0992318





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292542%2fhow-to-load-a-shared-component-lazily-in-angular%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo