我对injector
有疑问。
我构建了一个抽象类,它是自定义组件的基础。这就是我使用Injector
的构造函数的原因 - 对于简单的子类。
export abstract class SchematerLayout implements OnInit, AfterViewInit {
@Input() public form: FormGroup;
@Input() public field: IFieldConfig;
@ViewChild('fieldComponent', {read: ViewContainerRef}) fieldComponent: ViewContainerRef;
constructor(protected injector: Injector) {
}
ngOnInit() {
const componentResolver = this.injector.get(SchematerComponentResolverService);
const validationMessagesService = this.injector.get(SchematerValidationMessagesService);
}
}
问题是SchematerValidationMessagesService
总是错误Can't resolve all parameters for SchematerValidationMessagesService: (?).
而SchematerComponentResolverService
取决于相同的服务。
这是我的SchematerValidationMessagesService
课程:
@Injectable()
export class SchematerValidationMessagesService {
constructor(
protected configService: SchematerConfigService
) {
}
getErrorMessage(fieldControl: AbstractControl, fieldConfig: IFieldConfig) {
//...
}
}
SchematerComponentResolverService
课程:
@Injectable()
export class SchematerComponentResolverService {
constructor(protected componentFactoryResolver: ComponentFactoryResolver,
protected configService: SchematerConfigService
) {
}
createLayoutComponent(type: string) {
// console.log(type);
}
}
我正在通过forRoot
策略获得提供商:
static forRoot(config: SchematerConfig): ModuleWithProviders {
return {
ngModule: SchematerInputModule,
providers: [
{
provide: SchematerConfig,
useValue: config,
},
{
provide: ANALYZE_FOR_ENTRY_COMPONENTS,
useValue: config,
multi: true
},
SchematerConfigService,
SchematerValidationMessagesService,
SchematerComponentResolverService,
]
};
}
任何人都可以告诉我为什么SchematerConfigService
可用于首次服务而不是第二次服务?