What is the choice forRoot vs @Injectable providedIn metadata










0














I can use ModuleWithProviders forRoot static methods to register some services with roots modules since those services shared among few components within different modules.



export class SharedModule 
static forRoot(): ModuleWithProviders
return
ngModule: SharedModule,
providers: [ CounterService ]





But with angular 6 if a services is need to register with root injector simply can be done with @Injector decorator with providedIn metadata set as 'root'.



@Injectable(
providedIn: 'root'
)
export class UserService




So what approach should be appropriate means what would be way i should follow?



also does forRoot going to be obsolete??



What happen to Provider Array in Modules??










share|improve this question




























    0














    I can use ModuleWithProviders forRoot static methods to register some services with roots modules since those services shared among few components within different modules.



    export class SharedModule 
    static forRoot(): ModuleWithProviders
    return
    ngModule: SharedModule,
    providers: [ CounterService ]





    But with angular 6 if a services is need to register with root injector simply can be done with @Injector decorator with providedIn metadata set as 'root'.



    @Injectable(
    providedIn: 'root'
    )
    export class UserService




    So what approach should be appropriate means what would be way i should follow?



    also does forRoot going to be obsolete??



    What happen to Provider Array in Modules??










    share|improve this question


























      0












      0








      0







      I can use ModuleWithProviders forRoot static methods to register some services with roots modules since those services shared among few components within different modules.



      export class SharedModule 
      static forRoot(): ModuleWithProviders
      return
      ngModule: SharedModule,
      providers: [ CounterService ]





      But with angular 6 if a services is need to register with root injector simply can be done with @Injector decorator with providedIn metadata set as 'root'.



      @Injectable(
      providedIn: 'root'
      )
      export class UserService




      So what approach should be appropriate means what would be way i should follow?



      also does forRoot going to be obsolete??



      What happen to Provider Array in Modules??










      share|improve this question















      I can use ModuleWithProviders forRoot static methods to register some services with roots modules since those services shared among few components within different modules.



      export class SharedModule 
      static forRoot(): ModuleWithProviders
      return
      ngModule: SharedModule,
      providers: [ CounterService ]





      But with angular 6 if a services is need to register with root injector simply can be done with @Injector decorator with providedIn metadata set as 'root'.



      @Injectable(
      providedIn: 'root'
      )
      export class UserService




      So what approach should be appropriate means what would be way i should follow?



      also does forRoot going to be obsolete??



      What happen to Provider Array in Modules??







      javascript angular angular6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 12:31

























      asked Nov 12 at 11:46









      Amal Shalika

      94211




      94211






















          2 Answers
          2






          active

          oldest

          votes


















          0














          In angular when a module provides both declaration and providers it would duplicate the provider instances when injecting in child that would cause issues on instances which are probably meant to be singletons.
          For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.






          share|improve this answer




























            0














            providedIn is the new syntax to create singletons (i.e. providers declared in the root injector).



            You can provide it in any other module, but root is the shorthand for AppModule.



            It allows you to install dependencies pretty fast and without the need of any additional configuration.



            Hence, forRoot won't be obsolete, because it allows one to be able to configure a module through it.



            I'd say you should use the providedIn syntax for your providers, and if they need configuration, you can use forRoot.






            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%2f53261505%2fwhat-is-the-choice-forroot-vs-injectable-providedin-metadata%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              In angular when a module provides both declaration and providers it would duplicate the provider instances when injecting in child that would cause issues on instances which are probably meant to be singletons.
              For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.






              share|improve this answer

























                0














                In angular when a module provides both declaration and providers it would duplicate the provider instances when injecting in child that would cause issues on instances which are probably meant to be singletons.
                For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.






                share|improve this answer























                  0












                  0








                  0






                  In angular when a module provides both declaration and providers it would duplicate the provider instances when injecting in child that would cause issues on instances which are probably meant to be singletons.
                  For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.






                  share|improve this answer












                  In angular when a module provides both declaration and providers it would duplicate the provider instances when injecting in child that would cause issues on instances which are probably meant to be singletons.
                  For this reason Angular provides a way to separate providers out of the module so that same module can be imported into the root module with providers and child modules without providers.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 11:55









                  Haris

                  679




                  679























                      0














                      providedIn is the new syntax to create singletons (i.e. providers declared in the root injector).



                      You can provide it in any other module, but root is the shorthand for AppModule.



                      It allows you to install dependencies pretty fast and without the need of any additional configuration.



                      Hence, forRoot won't be obsolete, because it allows one to be able to configure a module through it.



                      I'd say you should use the providedIn syntax for your providers, and if they need configuration, you can use forRoot.






                      share|improve this answer

























                        0














                        providedIn is the new syntax to create singletons (i.e. providers declared in the root injector).



                        You can provide it in any other module, but root is the shorthand for AppModule.



                        It allows you to install dependencies pretty fast and without the need of any additional configuration.



                        Hence, forRoot won't be obsolete, because it allows one to be able to configure a module through it.



                        I'd say you should use the providedIn syntax for your providers, and if they need configuration, you can use forRoot.






                        share|improve this answer























                          0












                          0








                          0






                          providedIn is the new syntax to create singletons (i.e. providers declared in the root injector).



                          You can provide it in any other module, but root is the shorthand for AppModule.



                          It allows you to install dependencies pretty fast and without the need of any additional configuration.



                          Hence, forRoot won't be obsolete, because it allows one to be able to configure a module through it.



                          I'd say you should use the providedIn syntax for your providers, and if they need configuration, you can use forRoot.






                          share|improve this answer












                          providedIn is the new syntax to create singletons (i.e. providers declared in the root injector).



                          You can provide it in any other module, but root is the shorthand for AppModule.



                          It allows you to install dependencies pretty fast and without the need of any additional configuration.



                          Hence, forRoot won't be obsolete, because it allows one to be able to configure a module through it.



                          I'd say you should use the providedIn syntax for your providers, and if they need configuration, you can use forRoot.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 12:29









                          trichetriche

                          25.4k42051




                          25.4k42051



























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53261505%2fwhat-is-the-choice-forroot-vs-injectable-providedin-metadata%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

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              ReactJS Fetched API data displays live - need Data displayed static

                              政党