docs(core): update API doc examples to use static injector (#29729)

PR Close #29729
This commit is contained in:
Brandon 2019-04-05 16:33:26 +00:00 committed by Igor Minar
parent 632847d34d
commit 82c77ce232
3 changed files with 34 additions and 52 deletions

View File

@ -39,13 +39,8 @@ export interface InjectableDecorator {
*
* The following example shows how service classes are properly marked as
* injectable.
*
* {@example core/di/ts/metadata_spec.ts region='Injectable'}
*
* `Injector` throws an error if it tries to instantiate a class that
* is not decorated with `@Injectable`, as shown in the following example.
*
* {@example core/di/ts/metadata_spec.ts region='InjectableThrows'}
*Z
* <code-example path="core/di/ts/metadata_spec.ts" region="Injectable"></code-example>
*
*/
(): TypeDecorator;

View File

@ -26,12 +26,11 @@ export interface InjectDecorator {
* The following example shows a class constructor that specifies a
* custom provider of a dependency using the parameter decorator.
*
* {@example core/di/ts/metadata_spec.ts region='Inject'}
*
* When `@Inject()` is not present, the injector uses the type annotation of the
* parameter as the provider.
*
* {@example core/di/ts/metadata_spec.ts region='InjectWithoutDecorator'}
* <code-example path="core/di/ts/metadata_spec.ts"
* region="InjectWithoutDecorator"></code-example>
*/
(token: any): any;
new (token: any): Inject;
@ -78,7 +77,7 @@ export interface OptionalDecorator {
*
* The following code allows the possibility of a null result:
*
* {@example core/di/ts/metadata_spec.ts region='Optional'}
* <code-example path="core/di/ts/metadata_spec.ts" region="Optional"></code-example>
*
*/
(): any;
@ -119,7 +118,8 @@ export interface SelfDecorator {
* by the local injector when instantiating the class itself, but not
* when instantiating a child.
*
* {@example core/di/ts/metadata_spec.ts region='Self'}
* <code-example path="core/di/ts/metadata_spec.ts" region="Self"></code-example>
*
*
* @see `SkipSelf`
* @see `Optional`
@ -162,7 +162,7 @@ export interface SkipSelfDecorator {
* In the following example, the dependency can be resolved when
* instantiating a child, but not when instantiating the class itself.
*
* {@example core/di/ts/metadata_spec.ts region='SkipSelf'}
* <code-example path="core/di/ts/metadata_spec.ts" region="SkipSelf"></code-example>
*
* Learn more in the
* [Dependency Injection guide](guide/dependency-injection-in-action#skip).
@ -208,7 +208,7 @@ export interface HostDecorator {
*
* The following shows use with the `@Optional` decorator, and allows for a null result.
*
* {@example core/di/ts/metadata_spec.ts region='Host'}
* <code-example path="core/di/ts/metadata_spec.ts" region="Host"></code-example>
*/
(): any;
new (): Host;

View File

@ -6,28 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Component, Directive, Host, Inject, Injectable, Optional, ReflectiveInjector, Self, SkipSelf} from '@angular/core';
import {Component, Directive, Host, Injectable, Injector, Optional, Self, SkipSelf} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
{
describe('di metadata examples', () => {
describe('Inject', () => {
it('works', () => {
// #docregion Inject
class Engine {}
@Injectable()
class Car {
constructor(@Inject('MyEngine') public engine: Engine) {}
}
const injector =
ReflectiveInjector.resolveAndCreate([{provide: 'MyEngine', useClass: Engine}, Car]);
expect(injector.get(Car).engine instanceof Engine).toBe(true);
// #enddocregion
});
it('works without decorator', () => {
// #docregion InjectWithoutDecorator
class Engine {}
@ -38,7 +22,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
} // same as constructor(@Inject(Engine) engine:Engine)
}
const injector = ReflectiveInjector.resolveAndCreate([Engine, Car]);
const injector = Injector.create(
{providers: [{provide: Engine, deps: []}, {provide: Car, deps: [Engine]}]});
expect(injector.get(Car).engine instanceof Engine).toBe(true);
// #enddocregion
});
@ -54,7 +39,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
constructor(@Optional() public engine: Engine) {}
}
const injector = ReflectiveInjector.resolveAndCreate([Car]);
const injector =
Injector.create({providers: [{provide: Car, deps: [[new Optional(), Engine]]}]});
expect(injector.get(Car).engine).toBeNull();
// #enddocregion
});
@ -72,22 +58,14 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
constructor(public service: UsefulService) {}
}
const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]);
const injector = Injector.create({
providers: [
{provide: NeedsService, deps: [UsefulService]}, {provide: UsefulService, deps: []}
]
});
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
// #enddocregion
});
it('throws without Injectable', () => {
// #docregion InjectableThrows
class UsefulService {}
class NeedsService {
constructor(public service: UsefulService) {}
}
expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();
// #enddocregion
});
});
describe('Self', () => {
@ -100,13 +78,20 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
constructor(@Self() public dependency: Dependency) {}
}
let inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
let inj = Injector.create({
providers: [
{provide: Dependency, deps: []},
{provide: NeedsDependency, deps: [[new Self(), Dependency]]}
]
});
const nd = inj.get(NeedsDependency);
expect(nd.dependency instanceof Dependency).toBe(true);
inj = ReflectiveInjector.resolveAndCreate([Dependency]);
const child = inj.resolveAndCreateChild([NeedsDependency]);
const child = Injector.create({
providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
parent: inj
});
expect(() => child.get(NeedsDependency)).toThrowError();
// #enddocregion
});
@ -122,11 +107,13 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
}
const parent = ReflectiveInjector.resolveAndCreate([Dependency]);
const child = parent.resolveAndCreateChild([NeedsDependency]);
const parent = Injector.create({providers: [{provide: Dependency, deps: []}]});
const child =
Injector.create({providers: [{provide: NeedsDependency, deps: [Dependency]}], parent});
expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);
const inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
const inj = Injector.create(
{providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}]});
expect(() => inj.get(NeedsDependency)).toThrowError();
// #enddocregion
});