/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/core/src/di/injection_token.ts
133 строки
4 KB
SkyZeroZx
fix(core): avoid false-positive deprecation when using `InjectionToken` with factory only
11 дек 2025, 22:20
11 дек 2025, 22:20
4f6014a
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Type} from '../interface/type'; import {assertLessThan} from '../util/assert'; import {ɵɵdefineInjectable} from './interface/defs'; /** * Creates a token that can be used in a DI Provider. * * Use an `InjectionToken` whenever the type you are injecting is not reified (does not have a * runtime representation) such as when injecting an interface, callable type, array or * parameterized type. * * `InjectionToken` is parameterized on `T` which is the type of object which will be returned by * the `Injector`. This provides an additional level of type safety. * * <div class="docs-alert docs-alert-helpful"> * * **Important Note**: Ensure that you use the same instance of the `InjectionToken` in both the * provider and the injection call. Creating a new instance of `InjectionToken` in different places, * even with the same description, will be treated as different tokens by Angular's DI system, * leading to a `NullInjectorError`. * * </div> * * {@example injection-token/src/main.ts region='InjectionToken'} * * When creating an `InjectionToken`, you can optionally specify a factory function which returns * (possibly by creating) a default value of the parameterized type `T`. This sets up the * `InjectionToken` using this factory as a provider as if it was defined explicitly in the * application's root injector. If the factory function, which takes zero arguments, needs to inject * dependencies, it can do so using the [`inject`](api/core/inject) function. * As you can see in the Tree-shakable InjectionToken example below. * * Additionally, if a `factory` is specified you can also specify the `providedIn` option, which * overrides the above behavior and marks the token as belonging to a particular `@NgModule` (note: * this option is now deprecated). As mentioned above, `'root'` is the default value for * `providedIn`. * * The `providedIn: NgModule` and `providedIn: 'any'` options are deprecated. * * @usageNotes * ### Basic Examples * * ### Plain InjectionToken * * {@example core/di/ts/injector_spec.ts region='InjectionToken'} * * ### Tree-shakable InjectionToken * * {@example core/di/ts/injector_spec.ts region='ShakableInjectionToken'} * * * @see [What is an InjectionToken?](guide/di/defining-dependency-providers#what-is-an-injectiontoken) * * @publicApi */ export class InjectionToken<T> { /** @internal */ readonly ngMetadataName = 'InjectionToken'; readonly ɵprov: unknown; /** * @deprecated The `providedIn: NgModule` or `providedIn:'any'` options are deprecated. Please use the other signature. */ constructor( _desc: string, options: { providedIn: Type<any> | 'any'; factory: () => T; }, ); /** * @param _desc Description for the token, * used only for debugging purposes, * it should but does not need to be unique * @param options Options for the token's usage, as described above */ constructor( _desc: string, options?: { providedIn?: Type<any> | 'root' | 'platform' | 'any' | null; factory: () => T; }, ); constructor( protected _desc: string, options?: { providedIn?: Type<any> | 'root' | 'platform' | 'any' | null; factory: () => T; }, ) { this.ɵprov = undefined; if (typeof options == 'number') { (typeof ngDevMode === 'undefined' || ngDevMode) && assertLessThan(options, 0, 'Only negative numbers are supported here'); // This is a special hack to assign __NG_ELEMENT_ID__ to this instance. // See `InjectorMarkers` (this as any).__NG_ELEMENT_ID__ = options; } else if (options !== undefined) { this.ɵprov = ɵɵdefineInjectable({ token: this, providedIn: options.providedIn || 'root', factory: options.factory, }); } } /** * @internal */ get multi(): InjectionToken<Array<T>> { return this as InjectionToken<Array<T>>; } toString(): string { return `InjectionToken ${this._desc}`; } } export interface InjectableDefToken<T> extends InjectionToken<T> { ɵprov: unknown; }