自定义 ViewResolver 和依赖注入
Custom ViewResolver and dependency injection
我正在尝试创建自定义 ViewResolver
class(扩展 angular 的内置 class)以使用定制服务(我正在从外部系统加载我的样式)。然而,我 运行 遇到了依赖注入系统和 ViewResolver
的问题。
我的系统设置如下:
Boot.ts:
bootstrap(App, [
MyStyleService, // my custom service
SomeOtherService, // another custom service used by MyStyleService
{
provide: ViewResolver,
useClass: MyViewResolver // my custom ViewResolver
}
])
MyViewResolver.ts:
@Injectable()
export class MyViewResolover extends ViewResolver {
constructor(
private _reflector: ReflectorReader,
// I want to reference 'StyleService' from the providers array in boot.ts
private _styleService: StyleService
) {
super(_reflector);
}
public resolve(comopnent: Type) {
let meta: ViewMetadata = super.resolve(component);
let styles = this._styleService.someMethod(meta);
}
}
但是在 MyViewResolver
中,this._styleService
还没有被注入,目前是 undefined
。应该注意 MyStyleService
还依赖于另一个注入服务 SomeOtherService
,因此我需要确保该提供程序也已定义并且可用于注入器。
我希望 bootstrap 将所有这些服务 "provided",以便将来我可以在每个系统的基础上提供我的任何服务的替代版本。
作为参考,这是 angular 的核心 ViewResolver
:
view_resolver.ts(Angular2 核心):
import {Injectable, ViewMetadata, ComponentMetadata,} from '@angular/core';
import {ReflectorReader, reflector} from '../core_private';
import {Type, stringify, isBlank, isPresent} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Map} from '../src/facade/collection';
@Injectable()
export class ViewResolver {
constructor(private _reflector: ReflectorReader = reflector) {}
resolve(component: Type): ViewMetadata {
... stuff here ...
}
}
您可以尝试将 class 配置为 useFactory
:
bootstrap(App, [
MyStyleService,
SomeOtherService,
{
provide: ViewResolver,
useFactory: (_reflector: ReflectorReader, styleService: StyleService) => {
return MyViewResolver(_reflector, _styleService);
},
deps: [ ReflectorReader, StyleService ]
}
]);
我正在尝试创建自定义 ViewResolver
class(扩展 angular 的内置 class)以使用定制服务(我正在从外部系统加载我的样式)。然而,我 运行 遇到了依赖注入系统和 ViewResolver
的问题。
我的系统设置如下:
Boot.ts:
bootstrap(App, [
MyStyleService, // my custom service
SomeOtherService, // another custom service used by MyStyleService
{
provide: ViewResolver,
useClass: MyViewResolver // my custom ViewResolver
}
])
MyViewResolver.ts:
@Injectable()
export class MyViewResolover extends ViewResolver {
constructor(
private _reflector: ReflectorReader,
// I want to reference 'StyleService' from the providers array in boot.ts
private _styleService: StyleService
) {
super(_reflector);
}
public resolve(comopnent: Type) {
let meta: ViewMetadata = super.resolve(component);
let styles = this._styleService.someMethod(meta);
}
}
但是在 MyViewResolver
中,this._styleService
还没有被注入,目前是 undefined
。应该注意 MyStyleService
还依赖于另一个注入服务 SomeOtherService
,因此我需要确保该提供程序也已定义并且可用于注入器。
我希望 bootstrap 将所有这些服务 "provided",以便将来我可以在每个系统的基础上提供我的任何服务的替代版本。
作为参考,这是 angular 的核心 ViewResolver
:
view_resolver.ts(Angular2 核心):
import {Injectable, ViewMetadata, ComponentMetadata,} from '@angular/core';
import {ReflectorReader, reflector} from '../core_private';
import {Type, stringify, isBlank, isPresent} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Map} from '../src/facade/collection';
@Injectable()
export class ViewResolver {
constructor(private _reflector: ReflectorReader = reflector) {}
resolve(component: Type): ViewMetadata {
... stuff here ...
}
}
您可以尝试将 class 配置为 useFactory
:
bootstrap(App, [
MyStyleService,
SomeOtherService,
{
provide: ViewResolver,
useFactory: (_reflector: ReflectorReader, styleService: StyleService) => {
return MyViewResolver(_reflector, _styleService);
},
deps: [ ReflectorReader, StyleService ]
}
]);