我之前已经通过类似的方式实现了这一点:
你的库的服务应该被定义为一个接口而不是一个具体的实现(就像在 OO 语言中经常做的那样)。如果您的实现应用程序有时只想传入它自己的服务版本,那么您应该在您的库中创建一个默认服务,并像这样使用它:
import { Component, NgModule, ModuleWithProviders, Type, InjectionToken, Inject, Injectable } from '@angular/core';
export interface ILibService {
aFunction(): string;
}
export const LIB_SERVICE = new InjectionToken<ILibService>('LIB_SERVICE');
export interface MyLibConfig {
myService: Type<ILibService>;
}
@Injectable()
export class DefaultLibService implements ILibService {
aFunction() {
return 'default';
}
}
@Component({
// whatever
})
export class MyLibComponent {
constructor(@Inject(LIB_SERVICE) libService: ILibService) {
console.log(libService.aFunction());
}
}
@NgModule({
declarations: [MyLibComponent],
exports: [MyLibComponent]
})
export class LibModule {
static forRoot(config?: MyLibConfig): ModuleWithProviders {
return {
ngModule: LibModule,
providers: [
{ provide: LIB_SERVICE, useClass: config && config.myService || DefaultLibService }
]
};
}
}
然后在您的实现应用程序中,您可以通过库的forRoot 方法传入可选配置(注意forRoot 每个应用程序只应调用一次,并尽可能在最高级别调用)。请注意,我已将 config 参数标记为可选参数,因此即使您没有要传递的配置,您也应该调用 forRoot。
import { NgModule, Injectable } from '@angular/core';
import { LibModule, ILibService } from 'my-lib';
@Injectable()
export class OverridingService implements ILibService {
aFunction() {
return 'overridden!';
}
}
@NgModule({
imports: [LibModule.forRoot({ myService: OverridingService })]
})
export class ImplementingModule {
}
这是凭记忆写的,因为我目前手头没有代码,所以如果由于任何原因它不起作用,请告诉我。