What is the choice forRoot vs @Injectable providedIn metadata
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
add a comment |
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
add a comment |
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
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
javascript angular angular6
edited Nov 12 at 12:31
asked Nov 12 at 11:46
Amal Shalika
94211
94211
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 at 11:55
Haris
679
679
add a comment |
add a comment |
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
.
add a comment |
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
.
add a comment |
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
.
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
.
answered Nov 12 at 12:29
trichetriche
25.4k42051
25.4k42051
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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