简体   繁体   English

Angular 2 - 将Auth服务从AuthModule导出到AppModule

[英]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删除AuthServiceAuthGuard

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. 因此,您需要添加AuthComponentLoginComponent ,以便能够在其他模块中使用它们。

@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 . 还要注意的是导入AuthModuleAppModule ,不仅使提供给申报的其他组件的组件AppModule Any other modules that want to use these components should import the AuthModule . 任何其他想要使用这些组件的模块都应该导入AuthModule

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将服务从Angular2重写为Angular6“模块'AppModule'声明了意外的模块'HttpClientModule' - Rewriting services from Angular2 to Angular6 "Unexpected module 'HttpClientModule' declared by the module 'AppModule' 单元测试期间的 Auth0 Angular 错误:模块“DynamicTestModule”导入的值“AuthModule”异常。 请添加@NgModule 注释 - Auth0 Angular error during unit test: Unexpected value 'AuthModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation 在Angular 7中将工厂方法从AppModule移至AppComponent - Move factory method from AppModule to AppComponent in Angular 7 Angular 7 pass 路由器从 AppModule 到自定义库 - Angular 7 pass router from AppModule to custom library Angular 中的服务和导出功能之间的区别? - Difference between services and exporting functions in Angular? 从 Angular 5 升级到 Angular 7 后,找不到“AppModule”的 NgModule 元数据 - No NgModule metadata found for 'AppModule' after upgrade from Angular 5 to Angular 7 读取 AppModule 中的 Appsetting - Angular 8 - Reading Appsetting in AppModule - Angular 8 角度6中的StaticInjectorError(AppModule)窗口 - StaticInjectorError(AppModule) window in angular 6 错误:角度4或角度6中的StaticInjectorError(AppModule) - Error: StaticInjectorError(AppModule) in angular 4 or angular 6 如何从 AppModule 级别的共享库中获取提供的服务? Angular - How to get a provided service from shared library in AppModule level? Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM