如何从Angular 6中的另一个模块导入服务
技术:使用angular 6、angular cli和typescript 场景: 我有一个应用程序模块和一个核心模块 我希望我的应用程序模块组件使用我的核心模块中的服务 我的核心模块包含一个服务列表:如何从Angular 6中的另一个模块导入服务,angular,typescript,angular6,angular-module,Angular,Typescript,Angular6,Angular Module,技术:使用angular 6、angular cli和typescript 场景: 我有一个应用程序模块和一个核心模块 我希望我的应用程序模块组件使用我的核心模块中的服务 我的核心模块包含一个服务列表: import { NgModule } from '@angular/core'; // Services import { AuthService } from './services/auth.service'; @NgModule({ declarations: [ ],
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 { }
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 { 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 { }
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
) {}
}
注意:如您所见,我正在导入CoreModule
这是应用程序模块中的我的组件(它调用核心模块以获取AuthService):
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 { }
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
) {}
}
问题:我正在尝试将一项服务导入我的应用程序模块组件,名为contact component,如下所示:
import { AuthService } from '../../core/core.module';
我得到的错误:
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 { }
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
) {}
}
服务不是核心模块的导出成员
结果:我希望我的联系人组件从app module导入名为AuthService的核心模块服务。因为您在
AppModule
中导入了CoreModule
,在那里您还贴花了ContactComponent
(似乎在组件中),服务和组件都属于相同的模块,因此它们可以相互看到。在您的ContactComponent
中需要做的只是从实际位置导入服务AuthService
(而不是。/../core/core.module
)
import { AuthService } from ' <...>/auth.service';
从'/auth.service'导入{AuthService};
如果在根模块中正确导入模块,我认为如果您将“导出…从…”添加到mymodule.module.ts中:
import { MystuffService } from file 'mystuff.service'.
export { MystuffService } from file 'mystuff.service'.
然后,您可以导入您的服务:
import { MystuffService } from 'mymodule.module'
你不应该导出服务?@DeborahK为什么?你不应该或者你不能?你不需要。请参见此处的核心模块示例:请注意,它只导出组件,而不导出服务。@DeborahK,这是angular的默认行为还是最近更改的行为?真的非常感谢你的提示。@AngularM正确。实际上再也没有必要使用核心模块了,尤其是在可注入的装饰器(v6中新增)的属性中提供了新的。根本不需要在模块中注册服务。是的,样式指南需要更新。你的服务是什么样子的?(只是声明)你能回购吗?这是一次小规模的测试?