InjectionTokenlink
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 parametrized type.
InjectionToken
is parameterized on T
which is the type of object which will be returned by the Injector
. This provides additional level of type safety.
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
function. See below for an example.
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
. As mentioned above, 'root'
is the default value for providedIn
.
构造函数
参数
|
属性
属性名 | 类型 | 说明 |
---|---|---|
ngInjectableDef | 只读 | |
_desc | 声明于构造函数中 |
方法
参数没有参数。 返回值
|