Ionic3 - 将模块导入主应用程序模块 - 然后将它们注入服务/组件



如果我在app.module.ts中导入HttpClientModule

import { HttpClientModule } from '@angular/common/http';

并将其注入到应用程序@NgModule

@NgModule({
declarations: [
MyApp
],
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})

如何在其他页面、组件等中重用它?我应该再次导入它还是可以以某种方式从主应用程序模块注入它?

如果您将其注入主模块(即app.module.ts文件(,则无需在应用程序的其他任何地方注入。

您只需在必须在服务或组件中使用时导入httpClient

import { HttpClient } from '@angular/common/http';

最新更新