docs: update examples for tree-shakeable providers (#22961)

PR Close #22961
This commit is contained in:
Alex Rickabaugh 2018-03-23 08:36:48 -07:00
parent ed53c5ccdd
commit e1ea7ed019
25 changed files with 154 additions and 13 deletions

View File

@ -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);

View File

@ -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>
`
})

View File

@ -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';
@ -25,6 +26,7 @@ import { ProvidersModule } from './providers.module';
CarComponent,
HeroesComponent,
// #enddocregion ngmodule
HeroesTspComponent,
HeroListComponent,
InjectorComponent,
TestComponent

View File

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

View File

@ -1,6 +1,8 @@
import { Injectable } from '@angular/core';
@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {
constructor() { }
}

View File

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

View File

@ -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

View File

@ -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; }
}

View File

@ -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; }
}

View File

@ -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],
})
export class HeroService {
// #docregion internals
constructor(

View File

@ -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 { }

View File

@ -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 {
}

View File

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

View File

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

View File

@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';
// #docregion
@Injectable({
providedIn: 'root',
})
export class Service {
}

View File

@ -3,7 +3,7 @@
"files":[
"!**/*.d.ts",
"!**/*.js",
"!**/*.[0,1,2].*",
"!**/*.[0,1,2,3,4].*",
"!**/dummy.module.ts"
],
"tags": ["dependency", "di"]

View File

@ -6,7 +6,6 @@ import { UserService } from './user.service';
@NgModule({
imports: [ BrowserModule ],
providers: [ UserService ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})

View File

@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';
import { UserService } from './user.service';
@NgModule({
providers: [UserService],
})
export class UserModule {
}

View File

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

View File

@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';
import { UserModule } from './user.module';
@Injectable({
providedIn: UserModule,
})
export class UserService {
}

View File

@ -3,9 +3,7 @@ import { UserService } from './user.service';
describe('UserService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [UserService]
});
TestBed.configureTestingModule({});
});
it('should ...', inject([UserService], (service: UserService) => {

View File

@ -5,7 +5,9 @@ export class User {
name: string;
}
@Injectable()
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor() { }

View File

@ -3,7 +3,7 @@
"files": [
"!**/*.d.ts",
"!**/*.js",
"!**/*.[1,2].*"
"!**/*.[0,1,2].*"
],
"file": "src/app/app.component.ts",
"tags": ["providers"]

View File

@ -8,6 +8,5 @@ import { HeroService } from './heroes';
template: `
<toh-heroes></toh-heroes>
`,
providers: [HeroService]
})
export class AppComponent {}

View File

@ -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[] = [];