[英]Angular 2 - Exporting Auth Services from AuthModule to AppModule
I decided to put LoginComponent, AuthService, LoggedInGuard inside a module called AuthModule: 我决定将LoginComponent,AuthService,LoggedInGuard放在一个名为AuthModule的模块中:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AuthComponent } from './auth.component';
import { LoginComponent } from './components/login/login.component';
import { AuthService } from './services/auth/auth.service';
import { StorageService } from './services/storage/storage.service';
import { RequestService } from './services/request/request.service';
import { LoggedInGuard } from './guards/logged-in.guard';
@NgModule({
imports: [
CommonModule
],
providers: [AuthService, LoggedInGuard],
declarations: [AuthComponent, LoginComponent],
exports: [AuthService, LoggedInGuard]
})
export class AuthModule { }
And I want to use only AuthService methods in the rest of the Application. 我想在Application的其余部分中仅使用AuthService方法。 And LoggedInGuard to protect non-public routes. 和LoggedInGuard保护非公共路线。
So I tried to import them in AppModule: 所以我尝试在AppModule中导入它们:
import { AuthModule } from './core/auth/auth.module';
@NgModule({
declarations: [AppComponent, HomeComponent],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpModule,
AuthModule,
RouterModule.forRoot(routes)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
But in app.routes.ts LoggedInGuard is not available with this line of code: 但在app.routes.ts中,LoggedInGuard不适用于以下代码行:
import { LoggedInGuard } from './core/auth/auth.module';
It doesn't compile and it says: 它没有编译,它说:
...auth/auth.module has no exported member 'LoggedInGuard' ... auth / auth.module没有导出成员'LoggedInGuard'
If I point it to its right place: 如果我把它指向正确的位置:
import { LoggedInGuard } from './core/auth/guards/logged-in.guard';
It compiles, but is showing the following runtime error: 它编译,但显示以下运行时错误:
Unexpected value 'AuthService' exported by the module 'AuthModule' 模块'AuthModule'导出的意外值'AuthService'
What do you please suggest me? 你有什么建议我吗?
Thanks in advance. 提前致谢。
exports
isn't for services. exports
不是为了服务。 Adding the services to providers
is enough. 将服务添加到providers
就足够了。 So remove the AuthService
and AuthGuard
from the exports
. 因此,从exports
删除AuthService
和AuthGuard
。
What exports
is for is for making components, pipes, directives, and other modules accessible to other modules. exports
目的是使组件,管道,指令和其他模块可以访问其他模块。 So you need to add the AuthComponent
, and LoginComponent
to that is you want to be able to use them in other modules. 因此,您需要添加AuthComponent
和LoginComponent
,以便能够在其他模块中使用它们。
@NgModule({
imports: [
CommonModule
],
providers: [AuthService, LoggedInGuard],
declarations: [AuthComponent, LoginComponent],
exports: [AuthComponent, LoginComponent]
})
export class AuthModule { }
Also note that importing the AuthModule
to the AppModule
, only makes the components available to other components declared in the AppModule
. 还要注意的是导入AuthModule
到AppModule
,不仅使提供给申报的其他组件的组件AppModule
。 Any other modules that want to use these components should import the AuthModule
. 任何其他想要使用这些组件的模块都应该导入AuthModule
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.