角度注射器不通过deps

时间:2017-07-20 09:21:37

标签: angular

我对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可用于首次服务而不是第二次服务?

0 个答案:

没有答案