diff --git a/packages/core/src/di/injectable.ts b/packages/core/src/di/injectable.ts index 772dc88e40..d1675710ae 100644 --- a/packages/core/src/di/injectable.ts +++ b/packages/core/src/di/injectable.ts @@ -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 + * * */ (): TypeDecorator; diff --git a/packages/core/src/di/metadata.ts b/packages/core/src/di/metadata.ts index 33a6c94204..f5e55cfca1 100644 --- a/packages/core/src/di/metadata.ts +++ b/packages/core/src/di/metadata.ts @@ -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'} + * */ (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'} + * * */ (): 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'} + * + * * * @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'} + * * * 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'} + * */ (): any; new (): Host; diff --git a/packages/examples/core/di/ts/metadata_spec.ts b/packages/examples/core/di/ts/metadata_spec.ts index 635c7033bc..811790dd96 100644 --- a/packages/examples/core/di/ts/metadata_spec.ts +++ b/packages/examples/core/di/ts/metadata_spec.ts @@ -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 });