覆盖 root 中提供的 Angular 提供者

Ser*_*nho 7 angular angular-dependency-injection

我在一个惰性模块中导入了一个 ng-bootstrap 模态库。

@NgModule({imports: [NgbModalModule]})
Run Code Online (Sandbox Code Playgroud)

该库NgbModal在 root 中提供了一项服务。

@Injectable({providedIn: 'root'})
class NgbModal {...}
Run Code Online (Sandbox Code Playgroud)

我将它注入到一个组件中。

constructor(private modal: NgbModal) {}
Run Code Online (Sandbox Code Playgroud)

我开发了那个的类扩展。

export class CustomNgbModal extends NgbModal{...}
Run Code Online (Sandbox Code Playgroud)

如何使用 CustomNgbModal 覆盖 NgbModal 类型?

使用模块将是

{provide: NgbModal, useClass: CustomNgbModal}
Run Code Online (Sandbox Code Playgroud)

但是使用providedIn根元数据,没有线索。

那么,如何覆盖 root 中提供的模块?

And*_*rei -3

我相信你想要实现的目标可以通过

//module
providers: [
  CustomNgbModal, // so you can inject your custom service
  {provide: NgbModal, useExisting: CustomNgbModal} // to the library would use this service instead of its own
]
Run Code Online (Sandbox Code Playgroud)