docs: update examples to use correct `Injector.create()` overload (#42006)

The `Injector.create(providers, ...)` overload has been deprecated.
The examples now use the `Injector.create(options)` overload.

PR Close #42006
This commit is contained in:
MrJithil 2021-05-09 11:36:54 +05:30 committed by atscott
parent e86a1d3441
commit fc1bc0e0e8
1 changed files with 37 additions and 26 deletions

View File

@ -30,7 +30,7 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
describe('ValueProvider', () => { describe('ValueProvider', () => {
it('works', () => { it('works', () => {
// #docregion ValueProvider // #docregion ValueProvider
const injector = Injector.create([{provide: String, useValue: 'Hello'}]); const injector = Injector.create({providers: [{provide: String, useValue: 'Hello'}]});
expect(injector.get(String)).toEqual('Hello'); expect(injector.get(String)).toEqual('Hello');
// #enddocregion // #enddocregion
@ -41,10 +41,12 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
it('works', () => { it('works', () => {
// #docregion MultiProviderAspect // #docregion MultiProviderAspect
const locale = new InjectionToken<string[]>('locale'); const locale = new InjectionToken<string[]>('locale');
const injector = Injector.create([ const injector = Injector.create({
{provide: locale, multi: true, useValue: 'en'}, providers: [
{provide: locale, multi: true, useValue: 'sk'}, {provide: locale, multi: true, useValue: 'en'},
]); {provide: locale, multi: true, useValue: 'sk'},
]
});
const locales: string[] = injector.get(locale); const locales: string[] = injector.get(locale);
expect(locales).toEqual(['en', 'sk']); expect(locales).toEqual(['en', 'sk']);
@ -102,7 +104,8 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
name = 'square'; name = 'square';
} }
const injector = Injector.create([{provide: Shape, useClass: Square, deps: []}]); const injector =
Injector.create({providers: [{provide: Shape, useClass: Square, deps: []}]});
const shape: Shape = injector.get(Shape); const shape: Shape = injector.get(Shape);
expect(shape.name).toEqual('square'); expect(shape.name).toEqual('square');
@ -120,10 +123,12 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
salutation = 'Greetings'; salutation = 'Greetings';
} }
const injector = Injector.create([ const injector = Injector.create({
{provide: FormalGreeting, useClass: FormalGreeting, deps: []}, providers: [
{provide: Greeting, useClass: FormalGreeting, deps: []} {provide: FormalGreeting, useClass: FormalGreeting, deps: []},
]); {provide: Greeting, useClass: FormalGreeting, deps: []}
]
});
// The injector returns different instances. // The injector returns different instances.
// See: {provide: ?, useExisting: ?} if you want the same instance. // See: {provide: ?, useExisting: ?} if you want the same instance.
@ -159,9 +164,11 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
salutation = 'Greetings'; salutation = 'Greetings';
} }
const injector = Injector.create([ const injector = Injector.create({
{provide: FormalGreeting, deps: []}, {provide: Greeting, useExisting: FormalGreeting} providers: [
]); {provide: FormalGreeting, deps: []}, {provide: Greeting, useExisting: FormalGreeting}
]
});
expect(injector.get(Greeting).salutation).toEqual('Greetings'); expect(injector.get(Greeting).salutation).toEqual('Greetings');
expect(injector.get(FormalGreeting).salutation).toEqual('Greetings'); expect(injector.get(FormalGreeting).salutation).toEqual('Greetings');
@ -176,13 +183,15 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
const Location = new InjectionToken('location'); const Location = new InjectionToken('location');
const Hash = new InjectionToken('hash'); const Hash = new InjectionToken('hash');
const injector = Injector.create([ const injector = Injector.create({
{provide: Location, useValue: 'https://angular.io/#someLocation'}, { providers: [
provide: Hash, {provide: Location, useValue: 'https://angular.io/#someLocation'}, {
useFactory: (location: string) => location.split('#')[1], provide: Hash,
deps: [Location] useFactory: (location: string) => location.split('#')[1],
} deps: [Location]
]); }
]
});
expect(injector.get(Hash)).toEqual('someLocation'); expect(injector.get(Hash)).toEqual('someLocation');
// #enddocregion // #enddocregion
@ -193,12 +202,14 @@ import {Injectable, InjectionToken, Injector, Optional, ReflectiveInjector} from
const Location = new InjectionToken('location'); const Location = new InjectionToken('location');
const Hash = new InjectionToken('hash'); const Hash = new InjectionToken('hash');
const injector = Injector.create([{ const injector = Injector.create({
provide: Hash, providers: [{
useFactory: (location: string) => `Hash for: ${location}`, provide: Hash,
// use a nested array to define metadata for dependencies. useFactory: (location: string) => `Hash for: ${location}`,
deps: [[new Optional(), Location]] // use a nested array to define metadata for dependencies.
}]); deps: [[new Optional(), Location]]
}]
});
expect(injector.get(Hash)).toEqual('Hash for: null'); expect(injector.get(Hash)).toEqual('Hash for: null');
// #enddocregion // #enddocregion