New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOCS Update tutorial to recommend tree-shakeable providers #22934
Conversation
You can preview 589955d at https://pr22934-589955d.ngbuilds.io/. |
You can preview 2cf3585 at https://pr22934-2cf3585.ngbuilds.io/. |
You can preview ff82b4a at https://pr22934-ff82b4a.ngbuilds.io/. |
aio/content/tutorial/toh-pt4.md
Outdated
@@ -78,28 +78,35 @@ Add a `getHeroes` method to return the _mock heroes_. | |||
|
|||
You must _provide_ the `HeroService` in the _dependency injection system_ | |||
before Angular can _inject_ it into the `HeroesComponent`, | |||
as you will do [below](#inject). | |||
as you will do [below](#inject). A provider is something that can create or deliver a service; in this case, it instaniates the `HeroService` class to provide the service. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo instantiates
You can preview ae03b63 at https://pr22934-ae03b63.ngbuilds.io/. |
ae03b63
to
1c1f1ca
Compare
You can preview 6ee2e3b at https://pr22934-6ee2e3b.ngbuilds.io/. |
You can preview a2e7817 at https://pr22934-a2e7817.ngbuilds.io/. |
aio/content/tutorial/toh-pt4.md
Outdated
|
||
That's such a popular choice that you could have told the CLI to provide it there automatically | ||
by appending `--module=app`. | ||
You can register the `HeroService` as the provider of this service at different levels: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, the Angular CLI command ng generate service
registers a provider with the root injector for your service. When you provide it at the root level, Angular creates a single, shared instance of HeroService and injects into any class that asks for it. You can modify this and register your service at different levels (HeroesComponent, AppModule) based on your use case.
aio/content/tutorial/toh-pt4.md
Outdated
|
||
<code-example language="sh" class="code-shell"> | ||
ng generate service hero --module=app | ||
</code-example> | ||
|
||
Since you did not, you'll have to provide it yourself. | ||
Since you did not, you'll have to provide it yourself. A good practice is to provide it in the metadata for the service itself, which makes it available at a given level, but also allows for it to be removed from the app if it turns out not be used after all. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove: Since you did not, you'll have to provide it yourself. A good practice is to provide it in the metadata for the service itself, which makes it available at a given level, but also allows for it to be removed from the app if it turns out not be used after all.
aio/content/tutorial/toh-pt4.md
Outdated
|
||
Open the `AppModule` class, import the `HeroService`, and add it to the `@NgModule.providers` array. | ||
To do this, look for the `@Injectable()` statement right before the `HeroService` class definition, and add the `providedIn` metadata value.You typically provide a service at the root level, so that it can be used anywhere in the app: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
aio/content/tutorial/toh-pt4.md
Outdated
</code-example> | ||
|
||
The `providers` array tells Angular to create a single, shared instance of `HeroService` | ||
and inject into any class that asks for it. | ||
When you provide it at the root level, Angular creates a single, shared instance of `HeroService` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove
a2e7817
to
c460a55
Compare
You can preview c460a55 at https://pr22934-c460a55.ngbuilds.io/. |
You can preview 5c614bc at https://pr22934-5c614bc.ngbuilds.io/. |
You can preview 6efd5b0 at https://pr22934-6efd5b0.ngbuilds.io/. |
6efd5b0
to
10b7f85
Compare
You can preview 10b7f85 at https://pr22934-10b7f85.ngbuilds.io/. |
import { HEROES } from './mock-heroes'; | ||
|
||
// #docregion provider | ||
@Injectable({providedIn : root}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing quote for root
?
You can preview e73e1d9 at https://pr22934-e73e1d9.ngbuilds.io/. |
You can preview 80ffdbf at https://pr22934-80ffdbf.ngbuilds.io/. |
aio/content/tutorial/toh-pt4.md
Outdated
</code-example> | ||
``` | ||
@Injectable({providedIn: root}) | ||
``` | ||
|
||
When you provide a service at the root level, Angular creates a single, shared instance of `HeroService` and injects into any class that asks for it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove line 105. We are already mentioning this in the paragraph above.
aio/content/tutorial/toh-pt4.md
Outdated
@@ -442,7 +439,7 @@ Here are the code files discussed on this page and your app should look like thi | |||
## Summary | |||
|
|||
* You refactored data access to the `HeroService` class. | |||
* You _provided_ the `HeroService` in the root `AppModule` so that it can be injected anywhere. | |||
* You registered the `HeroService` as the _provider_ of its service as the root level, so that it can be injected anywhere in the app. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at the root level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rewrote this section, typo gone
aio/content/tutorial/toh-pt4.md
Outdated
|
||
The `providers` array tells Angular to create a single, shared instance of `HeroService` | ||
and inject into any class that asks for it. | ||
When you provide a service at the root level, Angular creates a single, shared instance of `HeroService` and injects into any class that asks for it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...creates a single, shared instance of
HeroService
and injects it into any class that asks for it.
This is also a duplicate of line 98 above.
Hi @jbogarthyde! This PR has merge conflicts due to recent upstream merges. |
8c6549f
to
a64bccb
Compare
aio/content/navigation.json
Outdated
@@ -462,11 +462,6 @@ | |||
} | |||
] | |||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still needs to be removed
aio/content/tutorial/toh-pt4.md
Outdated
@@ -40,19 +40,12 @@ The `HeroService` class should look like the below. | |||
### _@Injectable()_ services | |||
|
|||
Notice that the new service imports the Angular `Injectable` symbol and annotates | |||
the class with the `@Injectable()` decorator. | |||
the class with the `@Injectable()` decorator. This marks the class as one that participates in the _dependency injection system_, either by providing an injectable service or consuming one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove 'or consuming one'. This may have been old guidance to put @injectable
on anything that may consume a service, but it's not relevant anymore.
81b4006
to
580c13e
Compare
You can preview 81b4006 at https://pr22934-81b4006.ngbuilds.io/. |
You can preview 580c13e at https://pr22934-580c13e.ngbuilds.io/. |
580c13e
to
f571703
Compare
You can preview f571703 at https://pr22934-f571703.ngbuilds.io/. |
You can preview b96ee22 at https://pr22934-b96ee22.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we may need to squash the commits, but other than that, looks good!
aio/content/tutorial/toh-pt4.md
Outdated
@@ -442,7 +434,7 @@ Here are the code files discussed on this page and your app should look like thi | |||
## Summary | |||
|
|||
* You refactored data access to the `HeroService` class. | |||
* You _provided_ the `HeroService` in the root `AppModule` so that it can be injected anywhere. | |||
* You registered the `HeroService` as the _provider_ of its service as the root level so that it can be injected anywhere in the app. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as the root level -> at the root level
6545420
to
3636c0a
Compare
You can preview 6545420 at https://pr22934-6545420.ngbuilds.io/. |
You can preview 3636c0a at https://pr22934-3636c0a.ngbuilds.io/. |
3636c0a
to
0869d98
Compare
You can preview 0869d98 at https://pr22934-0869d98.ngbuilds.io/. |
You can preview 9789249 at https://pr22934-9789249.ngbuilds.io/. |
9789249
to
deef42f
Compare
deef42f
to
aefddc9
Compare
You can preview deef42f at https://pr22934-deef42f.ngbuilds.io/. |
You can preview aefddc9 at https://pr22934-aefddc9.ngbuilds.io/. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #21085, #20398
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Change example and surrounding text to show best practice of specifying provider in the service itself, to make it tree-shakeable.
Adds new version of code sample to show a second state.
Associated with PR 22803
Also fixes code samples to show import, addressing issues #21085, #20398