docs(core): update API doc examples to use static injector (#29729)
PR Close #29729
This commit is contained in:
parent
632847d34d
commit
82c77ce232
|
@ -39,13 +39,8 @@ export interface InjectableDecorator {
|
||||||
*
|
*
|
||||||
* The following example shows how service classes are properly marked as
|
* The following example shows how service classes are properly marked as
|
||||||
* injectable.
|
* injectable.
|
||||||
*
|
*Z
|
||||||
* {@example core/di/ts/metadata_spec.ts region='Injectable'}
|
* <code-example path="core/di/ts/metadata_spec.ts" region="Injectable"></code-example>
|
||||||
*
|
|
||||||
* `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'}
|
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
(): TypeDecorator;
|
(): TypeDecorator;
|
||||||
|
|
|
@ -26,12 +26,11 @@ export interface InjectDecorator {
|
||||||
* The following example shows a class constructor that specifies a
|
* The following example shows a class constructor that specifies a
|
||||||
* custom provider of a dependency using the parameter decorator.
|
* 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
|
* When `@Inject()` is not present, the injector uses the type annotation of the
|
||||||
* parameter as the provider.
|
* 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;
|
(token: any): any;
|
||||||
new (token: any): Inject;
|
new (token: any): Inject;
|
||||||
|
@ -78,7 +77,7 @@ export interface OptionalDecorator {
|
||||||
*
|
*
|
||||||
* The following code allows the possibility of a null result:
|
* 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;
|
(): any;
|
||||||
|
@ -119,7 +118,8 @@ export interface SelfDecorator {
|
||||||
* by the local injector when instantiating the class itself, but not
|
* by the local injector when instantiating the class itself, but not
|
||||||
* when instantiating a child.
|
* 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 `SkipSelf`
|
||||||
* @see `Optional`
|
* @see `Optional`
|
||||||
|
@ -162,7 +162,7 @@ export interface SkipSelfDecorator {
|
||||||
* In the following example, the dependency can be resolved when
|
* In the following example, the dependency can be resolved when
|
||||||
* instantiating a child, but not when instantiating the class itself.
|
* 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
|
* Learn more in the
|
||||||
* [Dependency Injection guide](guide/dependency-injection-in-action#skip).
|
* [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.
|
* 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;
|
(): any;
|
||||||
new (): Host;
|
new (): Host;
|
||||||
|
|
|
@ -6,28 +6,12 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* 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';
|
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
|
|
||||||
{
|
{
|
||||||
describe('di metadata examples', () => {
|
describe('di metadata examples', () => {
|
||||||
describe('Inject', () => {
|
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', () => {
|
it('works without decorator', () => {
|
||||||
// #docregion InjectWithoutDecorator
|
// #docregion InjectWithoutDecorator
|
||||||
class Engine {}
|
class Engine {}
|
||||||
|
@ -38,7 +22,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
} // same as constructor(@Inject(Engine) engine:Engine)
|
} // 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);
|
expect(injector.get(Car).engine instanceof Engine).toBe(true);
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
});
|
});
|
||||||
|
@ -54,7 +39,8 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
constructor(@Optional() public engine: Engine) {}
|
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();
|
expect(injector.get(Car).engine).toBeNull();
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
});
|
});
|
||||||
|
@ -72,22 +58,14 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
constructor(public service: UsefulService) {}
|
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);
|
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws without Injectable', () => {
|
|
||||||
// #docregion InjectableThrows
|
|
||||||
class UsefulService {}
|
|
||||||
|
|
||||||
class NeedsService {
|
|
||||||
constructor(public service: UsefulService) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();
|
|
||||||
// #enddocregion
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Self', () => {
|
describe('Self', () => {
|
||||||
|
@ -100,13 +78,20 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
constructor(@Self() public dependency: Dependency) {}
|
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);
|
const nd = inj.get(NeedsDependency);
|
||||||
|
|
||||||
expect(nd.dependency instanceof Dependency).toBe(true);
|
expect(nd.dependency instanceof Dependency).toBe(true);
|
||||||
|
|
||||||
inj = ReflectiveInjector.resolveAndCreate([Dependency]);
|
const child = Injector.create({
|
||||||
const child = inj.resolveAndCreateChild([NeedsDependency]);
|
providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
|
||||||
|
parent: inj
|
||||||
|
});
|
||||||
expect(() => child.get(NeedsDependency)).toThrowError();
|
expect(() => child.get(NeedsDependency)).toThrowError();
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
});
|
});
|
||||||
|
@ -122,11 +107,13 @@ import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||||
constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
|
constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
|
||||||
}
|
}
|
||||||
|
|
||||||
const parent = ReflectiveInjector.resolveAndCreate([Dependency]);
|
const parent = Injector.create({providers: [{provide: Dependency, deps: []}]});
|
||||||
const child = parent.resolveAndCreateChild([NeedsDependency]);
|
const child =
|
||||||
|
Injector.create({providers: [{provide: NeedsDependency, deps: [Dependency]}], parent});
|
||||||
expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);
|
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();
|
expect(() => inj.get(NeedsDependency)).toThrowError();
|
||||||
// #enddocregion
|
// #enddocregion
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue