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 examples for tree-shakeable providers #22961

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions aio/content/examples/dependency-injection/e2e/app.e2e-spec.ts
Expand Up @@ -178,6 +178,11 @@ describe('Dependency Injection Tests', function () {
expect(heroes.count()).toBeGreaterThan(0);
});

it('authorized user should have multiple authorized heroes with tree-shakeable HeroesService', function () {
let heroes = element.all(by.css('#tspAuthorized app-hero-list div'));
expect(heroes.count()).toBeGreaterThan(0);
});

it('authorized user should have secret heroes', function () {
let heroes = element.all(by.css('#authorized app-hero-list div'));
expect(heroes.count()).toBeGreaterThan(0);
Expand Down
Expand Up @@ -21,6 +21,7 @@ import { UserService } from './user.service';
<p>
<app-heroes id="authorized" *ngIf="isAuthorized"></app-heroes>
<app-heroes id="unauthorized" *ngIf="!isAuthorized"></app-heroes>
<app-heroes-tsp id="tspAuthorized" *ngIf="isAuthorized"></app-heroes-tsp>
<app-providers></app-providers>
`
})
Expand Down
Expand Up @@ -6,6 +6,7 @@ import { APP_CONFIG, HERO_DI_CONFIG } from './app.config';
import { AppComponent } from './app.component';
import { CarComponent } from './car/car.component';
import { HeroesComponent } from './heroes/heroes.component';
import { HeroesTspComponent } from './heroes/heroes-tsp.component';
import { HeroListComponent } from './heroes/hero-list.component';
import { InjectorComponent } from './injector.component';
import { Logger } from './logger.service';
Expand All @@ -25,6 +26,7 @@ import { ProvidersModule } from './providers.module';
CarComponent,
HeroesComponent,
// #enddocregion ngmodule
HeroesTspComponent,
HeroListComponent,
InjectorComponent,
TestComponent
Expand Down
@@ -0,0 +1,6 @@
// #docregion
import { NgModule } from '@angular/core';

@NgModule({})
export class HeroModule {
}
@@ -1,6 +1,8 @@
import { Injectable } from '@angular/core';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {
constructor() { }
}
Expand Up @@ -2,7 +2,9 @@
import { Injectable } from '@angular/core';
import { HEROES } from './mock-heroes';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {
getHeroes() { return HEROES; }
}
Expand Up @@ -3,7 +3,9 @@ import { Injectable } from '@angular/core';
import { HEROES } from './mock-heroes';
import { Logger } from '../logger.service';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {

// #docregion ctor
Expand Down
@@ -0,0 +1,13 @@
// #docregion
import { Injectable } from '@angular/core';
import { HEROES } from './mock-heroes';

@Injectable({
// we declare that this service should be created
// by the root application injector.

providedIn: 'root',
})
export class HeroService {
getHeroes() { return HEROES; }
}
@@ -0,0 +1,14 @@
// #docregion
import { Injectable } from '@angular/core';
import { HeroModule } from './hero.module';
import { HEROES } from './mock-heroes';

@Injectable({
// we declare that this service should be created
// by any injector that includes HeroModule.

providedIn: HeroModule,
})
export class HeroService {
getHeroes() { return HEROES; }
}
Expand Up @@ -2,8 +2,14 @@
import { Injectable } from '@angular/core';
import { HEROES } from './mock-heroes';
import { Logger } from '../logger.service';
import { UserService } from '../user.service';

@Injectable()
@Injectable({
providedIn: 'root',
useFactory: (logger: Logger, userService: UserService) =>
new HeroService(logger, userService.user.isAuthorized),
deps: [Logger, UserService],
Copy link
Member

Choose a reason for hiding this comment

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

The corresponding guide section should be updated accordingly: https://pr22961-854a794.ngbuilds.io/guide/dependency-injection#factory-providers

})
export class HeroService {
// #docregion internals
constructor(
Expand Down
@@ -0,0 +1,16 @@
import { Component } from '@angular/core';

/**
* A version of `HeroesComponent` that does not provide the `HeroService` (and thus relies on its
* `Injectable`-declared provider) in order to function.
*
* TSP stands for Tree-Shakeable Provider.
*/
@Component({
selector: 'app-heroes-tsp',
template: `
<h2>Heroes</h2>
<app-hero-list></app-hero-list>
`
})
export class HeroesTspComponent { }
@@ -0,0 +1,15 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule } from '@angular/router';
import { ServiceModule } from './service-and-module';

// #docregion
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot([]),
ServiceModule,
],
})
export class AppModule {
}
@@ -0,0 +1,14 @@
// #docregion
import { Injectable, NgModule } from '@angular/core';

@Injectable()
export class Service {
doSomething(): void {
}
}

@NgModule({
providers: [Service],
})
export class ServiceModule {
}
@@ -0,0 +1,11 @@
import { Injectable } from '@angular/core';

// #docregion
@Injectable({
providedIn: 'root',
useFactory: () => new Service('dependency'),
})
export class Service {
constructor(private dep: string) {
}
}
@@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';

// #docregion
@Injectable({
providedIn: 'root',
})
export class Service {
}
2 changes: 1 addition & 1 deletion aio/content/examples/dependency-injection/stackblitz.json
Expand Up @@ -3,7 +3,7 @@
"files":[
"!**/*.d.ts",
"!**/*.js",
"!**/*.[0,1,2].*",
"!**/*.[0,1,2,3,4].*",
"!**/dummy.module.ts"
],
"tags": ["dependency", "di"]
Expand Down
1 change: 0 additions & 1 deletion aio/content/examples/providers/src/app/app.module.ts
Expand Up @@ -6,7 +6,6 @@ import { UserService } from './user.service';

@NgModule({
imports: [ BrowserModule ],
providers: [ UserService ],
Copy link
Member

Choose a reason for hiding this comment

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

The corresponding guide section should be updated accordingly: https://pr22961-854a794.ngbuilds.io/guide/providers#create-a-service

Copy link
Member Author

Choose a reason for hiding this comment

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

Guide updates are happening in a separate PR.

declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
Expand Down
9 changes: 9 additions & 0 deletions aio/content/examples/providers/src/app/user.module.ts
@@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';
Copy link
Member

Choose a reason for hiding this comment

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

Where is this file used?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I should've given some context. This is the predecessor PR to the docs updates for tree-shakeable providers.


import { UserService } from './user.service';

@NgModule({
providers: [UserService],
})
export class UserModule {
}
7 changes: 7 additions & 0 deletions aio/content/examples/providers/src/app/user.service.0.ts
@@ -0,0 +1,7 @@
import { Injectable } from '@angular/core';
Copy link
Member

Choose a reason for hiding this comment

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

Where is this file used?


@Injectable({
providedIn: 'root',
})
export class UserService {
}
8 changes: 8 additions & 0 deletions aio/content/examples/providers/src/app/user.service.1.ts
@@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';
Copy link
Member

Choose a reason for hiding this comment

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

Where is this file used?

import { UserModule } from './user.module';

@Injectable({
providedIn: UserModule,
})
export class UserService {
}
4 changes: 1 addition & 3 deletions aio/content/examples/providers/src/app/user.service.spec.ts
Expand Up @@ -3,9 +3,7 @@ import { UserService } from './user.service';

describe('UserService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [UserService]
});
TestBed.configureTestingModule({});
});

it('should ...', inject([UserService], (service: UserService) => {
Expand Down
4 changes: 3 additions & 1 deletion aio/content/examples/providers/src/app/user.service.ts
Expand Up @@ -5,7 +5,9 @@ export class User {
name: string;
}

@Injectable()
@Injectable({
providedIn: 'root',
})
export class UserService {

constructor() { }
Expand Down
2 changes: 1 addition & 1 deletion aio/content/examples/providers/stackblitz.json
Expand Up @@ -3,7 +3,7 @@
"files": [
"!**/*.d.ts",
"!**/*.js",
"!**/*.[1,2].*"
"!**/*.[0,1,2].*"
],
"file": "src/app/app.component.ts",
"tags": ["providers"]
Expand Down
Expand Up @@ -8,6 +8,5 @@ import { HeroService } from './heroes';
template: `
<toh-heroes></toh-heroes>
`,
providers: [HeroService]
Copy link
Member

Choose a reason for hiding this comment

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

The correspoding recommendation should be changed accordingly: https://pr22961-854a794.ngbuilds.io/guide/styleguide#style-07-03.

Copy link
Member Author

Choose a reason for hiding this comment

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

Guide updates are happening in a separate PR.

})
export class AppComponent {}
Expand Up @@ -5,7 +5,9 @@ import { Observable, of } from 'rxjs';

import { Hero } from './hero.model';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {
getHeroes() {
let heroes: Hero[] = [];
Expand Down