Skip to content

Commit

Permalink
docs: update examples for tree-shakeable providers
Browse files Browse the repository at this point in the history
  • Loading branch information
alxhub committed Mar 27, 2018
1 parent ab348ee commit fd1f243
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 13 deletions.
5 changes: 5 additions & 0 deletions aio/content/examples/dependency-injection/e2e/app.e2e-spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// #docregion
import { NgModule } from '@angular/core';

@NgModule({})
export class HeroModule {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@angular/core';

@Injectable()
@Injectable({
providedIn: 'root',
})
export class HeroService {
constructor() { }
}
Original file line number Diff line number Diff line change
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; }
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
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],
})
export class HeroService {
// #docregion internals
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 { }
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// #docregion
import { Injectable, NgModule } from '@angular/core';

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

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

// #docregion
@Injectable({
providedIn: 'root',
useFactory: () => new Service('dependency'),
})
export class Service {
constructor(private dep: string) {
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { UserService } from './user.service';

@NgModule({
imports: [ BrowserModule ],
providers: [ UserService ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
Expand Down
9 changes: 9 additions & 0 deletions aio/content/examples/providers/src/app/user.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NgModule } from '@angular/core';

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class UserService {
}
8 changes: 8 additions & 0 deletions aio/content/examples/providers/src/app/user.service.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Injectable } from '@angular/core';
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ import { HeroService } from './heroes';
template: `
<toh-heroes></toh-heroes>
`,
providers: [HeroService]
})
export class AppComponent {}
Original file line number Diff line number Diff line change
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

0 comments on commit fd1f243

Please sign in to comment.