在依赖模块中提供不同的服务

时间:2018-03-14 13:32:09

标签: angular

我有两个模块FirstModuleSecondModule

通常是两个单独的模块,但SecondModule导入FirstModule

我还提供服务DummyService,如果我将其与FirstModule一起使用,则应该有所不同,如果我在SecondModule中使用它,则会有所不同。 问题是来自FirstModule Angular的组件使用来自SecondModule的服务。

我可以让Angular使用@NgModule中提供的服务吗?

以下是一些代码:

FirstModule

@NgModule({
    imports: [
        CommonModule
    ],
    providers: [
        {
            provide: DummyService,
            useClass: DummyFirstService,
        }
    ]
})
export class FirstModule {
}

SecondModule

@NgModule({
    imports: [
        CommonModule,
        FirstModule,
    ],
    providers: [
        {
            provide: DummyService,
            useClass: DummySecondService,
        }
    ]
})
export class SecondModule {
}

修改

我需要这个来扩展我构建的组件。 以下是来自FirstModule的组件代码:

@Component({
    selector: 'schemater-input-field',
    template: `
        <ng-container #fieldComponent></ng-container>`,
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => SchematerInputFieldComponent),
            multi: true
        },
        {
            provide: NG_VALIDATORS,
            useExisting: forwardRef(() => SchematerInputFieldComponent),
            multi: true
        }
    ]
})
export class SchematerInputFieldComponent implements ControlValueAccessor, AfterContentInit, OnInit, OnDestroy {
    protected _value: any;

    protected componentRef: ComponentRef<SchematerInputField>;
    protected _subscriptions: Subscription[] = [];

    @Input() public field: SchematerFieldConfig;
    @Input() protected formControlName: string;
    @Input() protected formGroup: FormGroup;
    @ViewChild('fieldComponent', {read: ViewContainerRef})
    fieldComponentContainer: ViewContainerRef;

    constructor(protected componentResolverService: ComponentResolverService) {
    }

    ngAfterContentInit() {
    }

    ngOnInit() {
        if (this.field && !this.field.id && this.formControlName) {
            this.field.id = this.formControlName;
        }

        //get correct component from service/config
        const fieldComponent = this.componentResolverService.createInputComponent(this.field.type); 
        this.componentRef = this.fieldComponentContainer.createComponent(fieldComponent);
        this.componentRef.instance.field = this.field;
        this.componentRef.instance.formGroup = this.formGroup;
        this.componentRef.instance.value = this._value;

        this._subscriptions.push(
            this.componentRef.instance.updateValue.subscribe(e => {
                this.value = e;
            })
        );
    }

    get value(): any {
        return this._value;
    }

    set value(value: any) {
        this._value = value;
        this.propagateChange(this._value);
    }

    writeValue(value: any) {
        if (value !== undefined) {
            this._value = value;
            if (this.componentRef) {
                this.componentRef.instance.value = this._value;
            }
        }
    }

    propagateChange = (_: any) => {
    };

    registerOnChange(fn) {
        this.propagateChange = fn;
    }

    registerOnTouched() {
    }

    validate(c: FormControl) {
    }

    ngOnDestroy() {
        this.componentRef.destroy();
        this._subscriptions.forEach(subscriber => subscriber.unsubscribe());
    }
}

SecondModule中,我只需要使用不同的constructor实例使用相同的代码。

export class SchematerSearchFieldComponent extends SchematerInputFieldComponent {}

也许还有其他方法可以使用不同的构造函数参数来扩展组件的类?

1 个答案:

答案 0 :(得分:1)

如果您希望在组件级别提供单独的服务实例,而不是在组件的提供者列表中传递服务名称。

示例:

组件1

@Component ({
    selector : 'comp1',
    template : `component1`,
    providers : [StateService]
})

组件2

@Component ({
    selector : 'comp2',
    template : `component2`,
    providers : [DummyService]
})

注意:正如您所指的那样,这可以提供帮助,在组件级别分离实例。