我决定将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 { }
我想在Application的其余部分中仅使用AuthService方法.并且LoggedInGuard保护非公共路线.
所以我尝试在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 { }
但在app.routes.ts中,LoggedInGuard不适用于以下代码行:
import { LoggedInGuard } from './core/auth/auth.module';
它没有编译,它说:
…auth/auth.module has no exported member ‘LoggedInGuard’
如果我把它指向正确的位置:
import { LoggedInGuard } from './core/auth/guards/logged-in.guard';
Unexpected value ‘AuthService’ exported by the module ‘AuthModule’
你有什么建议我吗?
提前致谢.
出口不是为了服务.将服务添加到提供商就足够了.因此,从导出中删除AuthService和AuthGuard.
原文链接:https://www.f2er.com/angularjs/141439.html导出的目的是使组件,管道,指令和其他模块可访问的其他模块.因此,您需要添加AuthComponent和LoginComponent,以便能够在其他模块中使用它们.
@NgModule({ imports: [ CommonModule ],exports: [AuthComponent,LoginComponent] }) export class AuthModule { }
另请注意,将AuthModule导入AppModule只会使组件可用于AppModule中声明的其他组件.任何其他想要使用这些组件的模块都应该导入AuthModule.