API
#
1. Decorators/** * Make class's dependencies become injectable. * *Technically, typescript require using a decorator to emit type.* */@Injectable()
/** * Allow to inject dependency with specific token. * *For now, it only allow to inject dependency via constructor.* */@Inject(token: Token)
/** * Config module meta data. */@Module(opts: ModuleOpts)
#
2. Container methods/** * Convenient method for adding multiple providers into this container. */addProviders(providers: GenericProvider[]): Promise<void>;
/** * Add a provider into this container for resolving dependency later. */addProvider(provider: GenericProvider): Promise<void>;
/** * Convenient method for adding multiple modules into this container. */addModules(modules: (ImportableCnf | ExportableCnf)[]): Promise<void>;
/** * Add a module into this container. */addModule(module: ImportableCnf | ExportableCnf): Promise<void>;
/** * Check if a provider with given token exists in this container. */has(token: Token): boolean;
/** * Remove provider from this container by token. */remove(token: Token): void;
/** * Resolve value by token. */resolve<T>(token: Token, options?: ResolveOptions): Promise<T>;
#
3. Types#
3.1. GenericProviderGenericProvider<T = any> = UseModuleProvider | UseClassProvider<T> | ClassType<T> | UseFuncProvider<T> | UseValueProvider<T>
:
Name | Type | Required | Default | Cacheable | Description |
---|---|---|---|---|---|
token | Token | True | Unique identifier for a provider inside a container. | ||
useValue | ValueType | Depending | Yesn't :v | Use a value as dependency value. Actually, you will get same value every time resolve() is invoked. | |
useFunc | FuncType | Depending | Yes | Use a function as factory to assemble dependency value. | |
useClass | ClassType | Depending | Yes | Use a class to resolve dependency value. | |
useModule | ClassType | Depending | Use a module as reference to resolve dependency value without import that module into current container. | ||
cycle | CycleType | False | 'transient' | Resolved value life cycle. It is only used for useClass , useFunc provider.- 'permanent': resolved value will be cached. - 'transient': resolved value will not be cached. | |
deps | any[] | False | Dependencies for useFunc provider. *Notice: - Dependencies's order must be same as useFunc parameter order. - Class will be used as a token for UseClassProvider provider, so you need to define its provider. |
#
3.2. ModuleOptsName | Type | Required |
---|---|---|
providers | GenericProvider[] | False |
imports | ImportableCnf[] | False |
exports | ExportableCnf[] | False |
#
3.3. ExtModuleOptsExtModuleOpts extends ModuleOpts
Name | Type | Required | Description |
---|---|---|---|
extModule | ClassType | True | Module class that you want to extend |
#
3.4. ResolveOptionsName | Type | Required | Description |
---|---|---|---|
extModule | Container | False |
#
3.5. OthersClassType<T> = { new(...args: any[]): T };
FuncType<T> = (...args: any[]) => T;
ValueType<T> = T;
Token = any;
ImportableCnf = ClassType<any> | ExtModuleOpts;
ExportableCnf = ClassType<any> | ExtModuleOpts;
CycleType = 'permanent' | 'transient';