@notadd/di
v3.2.0
Published
> TODO: description
Downloads
4
Keywords
Readme
@notadd/di
一款零依赖的
依赖注入
工具 - from angular
package size: 10.2 kB
unpacked size: 40.6 kB
static create
Injector.create([])
scope
injector 作用域
import { Injector } from '@notadd/di';
// scope = null
const nullInjector = Injector.create([])
// scope = root , parent scope = null
const injector = nullInjector.create([{provide: INJECTOR_SCOPE, useValue: 'root'}])
// scope = platform , parent scope = root , parent parent scope = null
const injector = injector.create([{provide: INJECTOR_SCOPE, useValue: 'platform'}])
multi provider
用于渐进式注入
import { rootInjector, InjectionToken } from '@notadd/di';
const token = new InjectionToken(`token`)
const inejctor = rootInjector.create([{ provide: INJECTOR_SCOPE, useValue: 'platform' }, { provide: token, useValue: 1, multi: true }])
const appModuleInjector = inejctor.create([
{ provide: token, useValue: 2, multi: true },
{ provide: token, useValue: 3, multi: true }
], 'AppModule')
// token wile be [1,2,3]
const tokens = appModuleInjector.get(token)
ValueProvider
使用指定值
rootInjector.create([{
provide: Car,
useValue: new Car()
}])
ConstructorProvider
快捷注入,使用自身
rootInjector.create([{
provide: Car,
deps: [CarNum]
}])
ExistingProvider
使用已注入的
rootInjector.create([{
provide: Car,
useExisting: Car2
}])
StaticClassProvider
使用useClass指定的类
rootInjector.create([{
provide: Car,
useClass: Car,
deps: [CarNum]
}])
FactoryProvider
使用指定factory进行创建
rootInjector.create([{
provide: Car,
useFactory: ()=>new Car(),
deps: []
}])
SkipSelf
跳过当前
rootInjector.create([{
provide: Car,
useFactory: ()=>new Car(),
deps: [new SkipSelf(), CarNum]
}])
Self
使用当前
rootInjector.create([{
provide: Car,
useFactory: ()=>new Car(),
deps: [new Self(), CarNum]
}])
Optional
当找不到时,不报错,可以为空
rootInjector.create([{
provide: Car,
useFactory: ()=>new Car(),
deps: [new Optional(), CarNum]
}])
组合使用
使用当前,并可以为空
rootInjector.create([{
provide: Car,
useFactory: ()=>new Car(),
deps: [new Self(), new Optional(), CarNum]
}])