4.2 KiB
4.2 KiB
@repo/core 依赖注入 API 参考
EnvironmentInjector
核心注入器类,管理依赖的注册和解析。
静态方法
// 创建根注入器 - 最顶层,无父注入器
static createRootInjector(providers: Provider[]): EnvironmentInjector
// 创建平台注入器 - 继承根注入器
static createPlatformInjector(providers: Provider[]): EnvironmentInjector
// 创建应用注入器 - 继承平台注入器
static createApplicationInjector(providers: Provider[]): EnvironmentInjector
// 创建特性注入器 - 需要指定父注入器
static createFeatureInjector(
providers: Provider[],
parent: Injector
): EnvironmentInjector
// 创建带自动提供者解析的注入器
static createWithAutoProviders(
providers: Provider[],
parent: Injector | null,
scope: InjectorScope
): EnvironmentInjector
实例方法
// 获取服务实例
get<T>(token: InjectionTokenType<T>, defaultValue?: T): T
// 动态注册提供者
set(providers: Provider[]): void
// 初始化 - 执行所有 @OnInit 和 APP_INITIALIZER
async init(): Promise<void>
// 销毁 - 执行所有 OnDestroy
async destroy(): Promise<void>
InjectionToken
类型安全的注入令牌。
class InjectionToken<T> {
constructor(description: string)
toString(): string
}
// 使用示例
const API_URL = new InjectionToken<string>('API_URL')
const CONFIG = new InjectionToken<AppConfig>('CONFIG')
HostAttributeToken
宿主属性令牌,用于获取宿主元素属性。
class HostAttributeToken<T> {
constructor(attributeName: string, defaultValue?: T)
}
// 使用示例
const dataAttr = new HostAttributeToken<string>('data-value', 'default')
装饰器
@Injectable(options?)
标记类为可注入服务。
interface InjectableOptions {
providedIn?: 'root' | 'platform' | 'application' | 'feature' | 'auto' | null
useFactory?: (...args: any[]) => any
deps?: any[]
}
@Injectable({ providedIn: 'root' })
class MyService {}
@Inject(token)
显式指定注入令牌。
constructor(@Inject(TOKEN) value: string) {}
@Optional(token)
可选注入,不存在时返回 null。
constructor(@Optional(OptionalService) service?: OptionalService) {}
@Self(token)
仅在当前注入器查找。
constructor(@Self(LocalService) service: LocalService) {}
@SkipSelf(token)
跳过当前注入器,从父注入器开始查找。
constructor(@SkipSelf(ParentService) service: ParentService) {}
@Host(token)
在宿主注入器中查找。
constructor(@Host(HostService) service: HostService) {}
@OnInit()
标记初始化方法。
@OnInit()
async onInit() {
await this.initialize()
}
生命周期接口
OnInit
interface OnInit {
onInit(): Promise<void>
}
OnDestroy
interface OnDestroy {
onDestroy(): Promise<void> | void
}
ForwardRef
解决循环依赖。
// 创建前向引用
function forwardRef<T>(fn: () => T): ForwardRef<T>
// 检查是否为前向引用
function isForwardRef(ref: any): boolean
// 解析前向引用
function resolveForwardRef<T>(ref: T | ForwardRef<T>): T
内部标志位
用于性能优化的位标志。
enum InternalInjectFlags {
Default = 0,
Optional = 1 << 0, // 1
SkipSelf = 1 << 1, // 2
Self = 1 << 2, // 4
Host = 1 << 3, // 8
}
// 工具函数
function combineInjectFlags(...flags: InternalInjectFlags[]): number
function hasFlag(flags: number, flag: InternalInjectFlags): boolean
function convertInjectOptionsToFlags(options: InjectOptions): number
类型定义
InjectionTokenType
type InjectionTokenType<T> =
| InjectionToken<T>
| HostAttributeToken<T>
| Type<T>
| AbstractType<T>
| StringToken<T>
| SymbolToken<T>
| Function
| ForwardRef<InjectionTokenType<T>>
InjectOptions
interface InjectOptions {
optional?: boolean
skipSelf?: boolean
self?: boolean
host?: boolean
}
InjectorScope
type InjectorScope = 'root' | 'platform' | 'application' | 'feature' | 'auto'