docs: update examples for tree-shakeable providers (#22961)
PR Close #22961
This commit is contained in:
parent
ed53c5ccdd
commit
e1ea7ed019
|
@ -178,6 +178,11 @@ describe('Dependency Injection Tests', function () {
|
||||||
expect(heroes.count()).toBeGreaterThan(0);
|
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 () {
|
it('authorized user should have secret heroes', function () {
|
||||||
let heroes = element.all(by.css('#authorized app-hero-list div'));
|
let heroes = element.all(by.css('#authorized app-hero-list div'));
|
||||||
expect(heroes.count()).toBeGreaterThan(0);
|
expect(heroes.count()).toBeGreaterThan(0);
|
||||||
|
|
|
@ -21,6 +21,7 @@ import { UserService } from './user.service';
|
||||||
<p>
|
<p>
|
||||||
<app-heroes id="authorized" *ngIf="isAuthorized"></app-heroes>
|
<app-heroes id="authorized" *ngIf="isAuthorized"></app-heroes>
|
||||||
<app-heroes id="unauthorized" *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>
|
<app-providers></app-providers>
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,6 +6,7 @@ import { APP_CONFIG, HERO_DI_CONFIG } from './app.config';
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
import { CarComponent } from './car/car.component';
|
import { CarComponent } from './car/car.component';
|
||||||
import { HeroesComponent } from './heroes/heroes.component';
|
import { HeroesComponent } from './heroes/heroes.component';
|
||||||
|
import { HeroesTspComponent } from './heroes/heroes-tsp.component';
|
||||||
import { HeroListComponent } from './heroes/hero-list.component';
|
import { HeroListComponent } from './heroes/hero-list.component';
|
||||||
import { InjectorComponent } from './injector.component';
|
import { InjectorComponent } from './injector.component';
|
||||||
import { Logger } from './logger.service';
|
import { Logger } from './logger.service';
|
||||||
|
@ -25,6 +26,7 @@ import { ProvidersModule } from './providers.module';
|
||||||
CarComponent,
|
CarComponent,
|
||||||
HeroesComponent,
|
HeroesComponent,
|
||||||
// #enddocregion ngmodule
|
// #enddocregion ngmodule
|
||||||
|
HeroesTspComponent,
|
||||||
HeroListComponent,
|
HeroListComponent,
|
||||||
InjectorComponent,
|
InjectorComponent,
|
||||||
TestComponent
|
TestComponent
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
// #docregion
|
||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
|
||||||
|
@NgModule({})
|
||||||
|
export class HeroModule {
|
||||||
|
}
|
|
@ -1,6 +1,8 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
constructor() { }
|
constructor() { }
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HEROES } from './mock-heroes';
|
import { HEROES } from './mock-heroes';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
getHeroes() { return HEROES; }
|
getHeroes() { return HEROES; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ import { Injectable } from '@angular/core';
|
||||||
import { HEROES } from './mock-heroes';
|
import { HEROES } from './mock-heroes';
|
||||||
import { Logger } from '../logger.service';
|
import { Logger } from '../logger.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
|
|
||||||
// #docregion ctor
|
// #docregion ctor
|
||||||
|
|
|
@ -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; }
|
||||||
|
}
|
|
@ -2,8 +2,14 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HEROES } from './mock-heroes';
|
import { HEROES } from './mock-heroes';
|
||||||
import { Logger } from '../logger.service';
|
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 {
|
export class HeroService {
|
||||||
// #docregion internals
|
// #docregion internals
|
||||||
constructor(
|
constructor(
|
||||||
|
|
|
@ -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 {
|
||||||
|
}
|
|
@ -3,7 +3,7 @@
|
||||||
"files":[
|
"files":[
|
||||||
"!**/*.d.ts",
|
"!**/*.d.ts",
|
||||||
"!**/*.js",
|
"!**/*.js",
|
||||||
"!**/*.[0,1,2].*",
|
"!**/*.[0,1,2,3,4].*",
|
||||||
"!**/dummy.module.ts"
|
"!**/dummy.module.ts"
|
||||||
],
|
],
|
||||||
"tags": ["dependency", "di"]
|
"tags": ["dependency", "di"]
|
||||||
|
|
|
@ -6,7 +6,6 @@ import { UserService } from './user.service';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [ BrowserModule ],
|
imports: [ BrowserModule ],
|
||||||
providers: [ UserService ],
|
|
||||||
declarations: [ AppComponent ],
|
declarations: [ AppComponent ],
|
||||||
bootstrap: [ AppComponent ]
|
bootstrap: [ AppComponent ]
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
import { NgModule } from '@angular/core';
|
||||||
|
|
||||||
|
import { UserService } from './user.service';
|
||||||
|
|
||||||
|
@NgModule({
|
||||||
|
providers: [UserService],
|
||||||
|
})
|
||||||
|
export class UserModule {
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class UserService {
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { UserModule } from './user.module';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: UserModule,
|
||||||
|
})
|
||||||
|
export class UserService {
|
||||||
|
}
|
|
@ -3,9 +3,7 @@ import { UserService } from './user.service';
|
||||||
|
|
||||||
describe('UserService', () => {
|
describe('UserService', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({});
|
||||||
providers: [UserService]
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should ...', inject([UserService], (service: UserService) => {
|
it('should ...', inject([UserService], (service: UserService) => {
|
||||||
|
|
|
@ -5,7 +5,9 @@ export class User {
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
export class UserService {
|
export class UserService {
|
||||||
|
|
||||||
constructor() { }
|
constructor() { }
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
"files": [
|
"files": [
|
||||||
"!**/*.d.ts",
|
"!**/*.d.ts",
|
||||||
"!**/*.js",
|
"!**/*.js",
|
||||||
"!**/*.[1,2].*"
|
"!**/*.[0,1,2].*"
|
||||||
],
|
],
|
||||||
"file": "src/app/app.component.ts",
|
"file": "src/app/app.component.ts",
|
||||||
"tags": ["providers"]
|
"tags": ["providers"]
|
||||||
|
|
|
@ -8,6 +8,5 @@ import { HeroService } from './heroes';
|
||||||
template: `
|
template: `
|
||||||
<toh-heroes></toh-heroes>
|
<toh-heroes></toh-heroes>
|
||||||
`,
|
`,
|
||||||
providers: [HeroService]
|
|
||||||
})
|
})
|
||||||
export class AppComponent {}
|
export class AppComponent {}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import { Observable, of } from 'rxjs';
|
||||||
|
|
||||||
import { Hero } from './hero.model';
|
import { Hero } from './hero.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
export class HeroService {
|
export class HeroService {
|
||||||
getHeroes() {
|
getHeroes() {
|
||||||
let heroes: Hero[] = [];
|
let heroes: Hero[] = [];
|
||||||
|
|
Loading…
Reference in New Issue