Skip to content
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

Closed

Conversation

jbogarthyde
Copy link
Contributor

@jbogarthyde jbogarthyde commented Mar 22, 2018

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[x] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: #21085, #20398

What is the new behavior?

Does this PR introduce a breaking change?

[ ] Yes
[x] No

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

@mary-poppins
Copy link

You can preview 589955d at https://pr22934-589955d.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 2cf3585 at https://pr22934-2cf3585.ngbuilds.io/.

@mary-poppins
Copy link

You can preview ff82b4a at https://pr22934-ff82b4a.ngbuilds.io/.

@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo instantiates

@mary-poppins
Copy link

You can preview ae03b63 at https://pr22934-ae03b63.ngbuilds.io/.

@jbogarthyde jbogarthyde added the target: major This PR is targeted for the next major release label Mar 30, 2018
@mary-poppins
Copy link

You can preview 6ee2e3b at https://pr22934-6ee2e3b.ngbuilds.io/.

@mary-poppins
Copy link

You can preview a2e7817 at https://pr22934-a2e7817.ngbuilds.io/.


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:
Copy link
Contributor

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.


<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.
Copy link
Contributor

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.


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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

</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`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

@mary-poppins
Copy link

You can preview c460a55 at https://pr22934-c460a55.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 5c614bc at https://pr22934-5c614bc.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 6efd5b0 at https://pr22934-6efd5b0.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 10b7f85 at https://pr22934-10b7f85.ngbuilds.io/.

import { HEROES } from './mock-heroes';

// #docregion provider
@Injectable({providedIn : root})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing quote for root?

@mary-poppins
Copy link

You can preview e73e1d9 at https://pr22934-e73e1d9.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 80ffdbf at https://pr22934-80ffdbf.ngbuilds.io/.

</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.
Copy link
Contributor

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.

alxhub
alxhub previously requested changes Apr 3, 2018
@@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at the root level

Copy link
Contributor Author

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


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.
Copy link
Member

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.

@ngbot
Copy link

ngbot bot commented Apr 3, 2018

Hi @jbogarthyde! This PR has merge conflicts due to recent upstream merges.
Please help to unblock it by resolving these conflicts. Thanks!

@@ -462,11 +462,6 @@
}
]
},
Copy link
Contributor

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

@@ -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.
Copy link
Contributor

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.

@mary-poppins
Copy link

You can preview 81b4006 at https://pr22934-81b4006.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 580c13e at https://pr22934-580c13e.ngbuilds.io/.

@mary-poppins
Copy link

You can preview f571703 at https://pr22934-f571703.ngbuilds.io/.

@mary-poppins
Copy link

You can preview b96ee22 at https://pr22934-b96ee22.ngbuilds.io/.

Copy link
Contributor

@StephenFluin StephenFluin left a 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!

@@ -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.
Copy link
Contributor

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

@mary-poppins
Copy link

You can preview 6545420 at https://pr22934-6545420.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 3636c0a at https://pr22934-3636c0a.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 0869d98 at https://pr22934-0869d98.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 9789249 at https://pr22934-9789249.ngbuilds.io/.

@mary-poppins
Copy link

You can preview deef42f at https://pr22934-deef42f.ngbuilds.io/.

@mary-poppins
Copy link

You can preview aefddc9 at https://pr22934-aefddc9.ngbuilds.io/.

@jbogarthyde jbogarthyde added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Apr 6, 2018
@IgorMinar IgorMinar closed this in 1fac5f4 Apr 7, 2018
@jbogarthyde jbogarthyde deleted the jb-tutorial-providers branch April 27, 2018 17:10
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet