
客户端
Angular 10 Swagger Codegen: 通用类型 ModuleWithProviders
typescriptexport interface MyModuleProviders { service1: Service1; service2: Service2; // ...}2. 创建一个模块,并在模块的静态方法中返回ModuleWithProviderstypescript@NgModule({ imports: [CommonModule], providers: [Service1, Service2],})export class MyModule { static forRoot(): ModuleWithProviders<MyModuleProviders> { return { ngModule: MyModule, providers: [ { provide: 'service1', useClass: Service1 }, { provide: 'service2', useClass: Service2 }, ], }; }}3. 在应用的根模块中导入MyModule时,调用forRoot()方法,并将提供者的实现传递给模块:typescript@NgModule({ declarations: [AppComponent], imports: [BrowserModule, MyModule.forRoot()], bootstrap: [AppComponent],})export class AppModule {}## 案例代码:使用ModuleWithProviderstypescriptexport interface ProductModuleProviders { productService: ProductService; cartService: CartService;}然后,我们创建一个ProductModule,并在静态方法forRoot()中返回ModuleWithProviderstypescript@NgModule({ imports: [CommonModule],})export class ProductModule { static forRoot(): ModuleWithProviders<ProductModuleProviders> { return { ngModule: ProductModule, providers: [ { provide: 'productService', useClass: ProductService }, { provide: 'cartService', useClass: CartService }, ], }; }}最后,在应用的根模块中导入ProductModule时,调用forRoot()方法,并将提供者的实现传递给模块:typescript@NgModule({ declarations: [AppComponent], imports: [BrowserModule, ProductModule.forRoot()], bootstrap: [AppComponent],})export class AppModule {}通过使用ModuleWithProvidersCopyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号