Angular2 模块没有导出成员

新手上路,请多包涵

对于在 Angular2 中进行身份验证的网站,我想在主应用程序组件中使用身份验证子模块的组件。但是,我不断收到以下错误:

app/app.component.ts(3,10): error TS2305: Module '"<dir>/app/auth/auth.module"' has no exported member 'SigninComponent'.

即使在导出 SigninComponent 之后。

项目文件夹结构如下图:

在此处输入图像描述

应用程序/auth/auth.module.ts:

 import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { FormsModule }    from '@angular/forms';

import { RegisterComponent }    from './components/register.component';
import { SigninComponent }    from './components/signin.component';
import { HomeComponent }    from './components/home.component';

import { AuthService } from './services/auth.service';
import { AuthHttp } from './services/auth-http';

@NgModule({
  imports: [
      CommonModule,
      FormsModule
  ],
  declarations: [
      RegisterComponent,
      SigninComponent,
      HomeComponent
  ],
  providers: [
      AuthService,
      AuthHttp
  ],
  exports: [
      RegisterComponent,
      SigninComponent,
      HomeComponent
  ]
})
export class AuthModule {}

app/auth/components/signin.component.ts:

 import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';

@Component({
    selector: 'signin',
    templateUrl: 'app/auth/signin.html'
})
export class SigninComponent {
    ...
}

应用程序/app.component.ts:

 import { Component, OnInit } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { SigninComponent, RegisterComponent } from './auth/auth.module';
import { AuthHttp } from './auth/services/auth-http';
import { AuthService } from './auth/services/auth.service';

@Component({
    selector: 'myapp',
    templateUrl: 'app/app.html'
})

export class AppComponent implements OnInit {
    ...
}

应用程序/app.module.ts:

 import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { HttpModule }    from '@angular/http';
import { RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';
import { AuthModule } from './auth/auth.module';

import { AppComponent } from './app.component';

import { AuthService } from './auth/services/auth.service';
import { AuthHttp } from './auth/services/auth-http';

@NgModule({
  declarations: [
      AppComponent,
      AuthService,
      AuthHttp
  ],
  bootstrap: [ AppComponent ],
  imports : [
      BrowserModule,
      FormsModule,
      HttpModule,
      AuthModule,
      AppRoutingModule
  ],
  providers: [
      AuthService,
      AuthHttp
  ]
})
export class AppModule {
}

原文由 user2416984 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

您不需要该行:

 import { SigninComponent, RegisterComponent } from './auth/auth.module';

在您的 app.component.ts 中,因为您已经在 AuthModule app.module.tsAutModule import 足以在应用程序中使用您的组件。

您得到的错误是 TypeScript 错误,而不是 Angular 错误,并且声明没有导出成员是正确的,因为它搜索有效的 EC6 语法进行导出,而不是 Angular 模块导出。因此,这条线将适用于您的 app.component.ts

 import { SigninComponent } from './auth/components/signin.component';

原文由 Fjut 发布,翻译遵循 CC BY-SA 3.0 许可协议

就我而言,我重新启动了 服务器 并且它工作了。

原文由 Amede Angel Aulerien 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题