docs: add explanation for providedIn any (#35283)

Angular 9 introduces a new value for providedIn called `any` which lets us use unique instance
for servicec in each lazy loaded module, this PR is to document the same

fixes #35179

PR Close #35283
This commit is contained in:
Santosh Yadav 2020-07-08 23:41:20 +05:30 committed by atscott
parent fb735d625b
commit 209a365511
4 changed files with 19 additions and 0 deletions

View File

@ -366,6 +366,7 @@ groups:
'aio/content/images/guide/pipes/**',
'aio/content/guide/providers.md',
'aio/content/examples/providers/**',
'aio/content/images/guide/providers/**',
'aio/content/guide/singleton-services.md',
'aio/content/guide/set-document-title.md',
'aio/content/examples/set-document-title/**',

View File

@ -0,0 +1,7 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'any',
})
export class UserService {
}

View File

@ -52,6 +52,14 @@ Any component created within a lazy loaded modules context, such as by router
Though you can provide services by lazy loading modules, not all services can be lazy loaded. For instance, some modules only work in the root module, such as the Router. The Router works with the global location object in the browser.
As of Angular version 9, you can provide a new instance of a service with each lazy loaded module. The following code adds this functionality to `UserService`.
<code-example path="providers/src/app/user.service.2.ts" header="src/app/user.service.ts"></code-example>
With `providedIn: 'any'`, all eagerly loaded modules share a singleton instance; however, lazy loaded modules each get their own unique instance, as shown in the following diagram.
<img src="generated/images/guide/providers/any-provider.svg" alt="any-provider-scope" class="left">
## Limiting provider scope with components

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 9.6 KiB