如何从 Angular 6 中的另一个模块导入服务
How to import a service from another module in Angular 6
技术:使用angular 6,angular cli 和打字稿。
场景:
我有一个 app 模块 和一个 core 模块
我希望我的应用程序模块组件使用核心模块中的服务。
我的核心模块包含一个服务列表:
import { NgModule } from '@angular/core';
// Services
import { AuthService } from './services/auth.service';
@NgModule({
declarations: [
],
imports: [
],
providers: [
AuthService
]
})
export class CoreModule { }
这是我的应用程序模块:
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
// Modules
import { CoreModule } from './core/core.module';
// Sections
import { COMPONENTS } from './components/index';
import { ROUTES } from './app.routes';
@NgModule({
declarations: [
AppComponent,
COMPONENTS
imports: [
CommonModule,
FormsModule,
BrowserModule,
HttpClientModule,
ReactiveFormsModule,
RouterModule.forRoot(ROUTES),
CoreModule
],
providers: [
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
注意:如您所见,我正在导入 CoreModule。
这是我的应用程序模块组件(它调用核心模块来获取 AuthService):
import { Component } from '@angular/core';
// This Auth Service import not working (says service not exported)
import { AuthService } from '../../core/core.module';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html'
})
export class ContactComponent {
constructor(
private authService: AuthService
) {}
}
问题:我正在尝试将服务导入到我的应用程序模块组件中,称为联系人组件,如下所示:
import { AuthService } from '../../core/core.module';
错误我得到:
服务不是核心模块的导出成员。
结果:我希望应用程序模块中的联系人组件导入名为 AuthService 的核心模块服务。
由于您在 AppModule
中导入了 CoreModule
,同时您还贴标了 ContactComponent
(似乎在 COMPONENTS
中),所以 service和component在这里属于同一个Module,所以可以互相看到。在您的 ContactComponent
中需要做的仅仅是 从其真实位置(而不是 ../../core/core.module
).[=18= 导入服务 AuthService
]
import { AuthService } from ' <...>/auth.service';
如果模块在根模块中正确导入,我认为如果您将“export ... from ...”添加到您的 mymodule.module.ts:
import { MystuffService } from file 'mystuff.service'.
export { MystuffService } from file 'mystuff.service'.
然后您可以导入您的服务:
import { MystuffService } from 'mymodule.module'
技术:使用angular 6,angular cli 和打字稿。
场景:
我有一个 app 模块 和一个 core 模块
我希望我的应用程序模块组件使用核心模块中的服务。
我的核心模块包含一个服务列表:
import { NgModule } from '@angular/core';
// Services
import { AuthService } from './services/auth.service';
@NgModule({
declarations: [
],
imports: [
],
providers: [
AuthService
]
})
export class CoreModule { }
这是我的应用程序模块:
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
// Modules
import { CoreModule } from './core/core.module';
// Sections
import { COMPONENTS } from './components/index';
import { ROUTES } from './app.routes';
@NgModule({
declarations: [
AppComponent,
COMPONENTS
imports: [
CommonModule,
FormsModule,
BrowserModule,
HttpClientModule,
ReactiveFormsModule,
RouterModule.forRoot(ROUTES),
CoreModule
],
providers: [
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
注意:如您所见,我正在导入 CoreModule。
这是我的应用程序模块组件(它调用核心模块来获取 AuthService):
import { Component } from '@angular/core';
// This Auth Service import not working (says service not exported)
import { AuthService } from '../../core/core.module';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html'
})
export class ContactComponent {
constructor(
private authService: AuthService
) {}
}
问题:我正在尝试将服务导入到我的应用程序模块组件中,称为联系人组件,如下所示:
import { AuthService } from '../../core/core.module';
错误我得到:
服务不是核心模块的导出成员。
结果:我希望应用程序模块中的联系人组件导入名为 AuthService 的核心模块服务。
由于您在 AppModule
中导入了 CoreModule
,同时您还贴标了 ContactComponent
(似乎在 COMPONENTS
中),所以 service和component在这里属于同一个Module,所以可以互相看到。在您的 ContactComponent
中需要做的仅仅是 从其真实位置(而不是 ../../core/core.module
).[=18= 导入服务 AuthService
]
import { AuthService } from ' <...>/auth.service';
如果模块在根模块中正确导入,我认为如果您将“export ... from ...”添加到您的 mymodule.module.ts:
import { MystuffService } from file 'mystuff.service'.
export { MystuffService } from file 'mystuff.service'.
然后您可以导入您的服务:
import { MystuffService } from 'mymodule.module'