简体   繁体   English

Angular2 模块没有导出成员

[英]Angular2 module has no exported member

For a website with authentication in Angular2, I want to use a component of the authentication submodule in the main app component.对于在 Angular2 中进行身份验证的网站,我想在主应用程序组件中使用身份验证子模块的一个组件。 However, I keep getting the following error:但是,我不断收到以下错误:

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

even after exporting SigninComponent.即使在导出 SigninComponent 之后。

The project folder structure is as shown below:项目文件夹结构如下图:

在此处输入图片说明

app/auth/auth.module.ts: app/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: 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/app.component.ts:应用程序/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/app.module.ts:应用程序/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 {
}

Working with atom (1.21.1 ia32)... i got the same error, even though i added a reference to my pipe in the app.module.ts and in the declarations within app.module.ts使用原子(1.21.1 ia32)...我得到了同样的错误,即使我在 app.module.ts 和 app.module.ts 的声明中添加了对我的管道的引用

solution was to restart my node instance... stopping the website and then doing ng serve again... going to localhost:4200 worked like a charm after this restart解决方案是重新启动我的节点实例...停止网站,然后再次执行ng serve ... 重新启动后转到 localhost:4200 就像一个魅力

You do not need the line:您不需要该行:

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

in your app.component.ts as you already included the AuthModule in your app.module.ts .在您的app.component.ts因为您已经在AuthModule中包含了app.module.ts AutModule import is sufficient to use your component in the app. AutModule导入足以在应用程序中使用您的组件。

The error that you get is a TypeScript error, not a Angular one, and it is correct in stating that there is no exported member, as it searches for a valid EC6 syntax for export, not angular module export.您得到的错误是 TypeScript 错误,而不是 Angular 错误,并且声明没有导出成员是正确的,因为它搜索有效的 EC6 导出语法,而不是 angular 模块导出。 This line would thus work in your app.component.ts :因此,这一行将在您的app.component.ts工作:

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

Also some of common cases :还有一些常见的情况:

maybe you export class with "default" prefix like so也许你像这样导出带有“默认”前缀的类

export default class Module {}

just remove it删除它

export class Module {}

this is solve the issue for me这是为我解决问题

对我来说,当我在单个.ts文件中有多个export语句时会出现这样的问题......

This error can also occur if your interface name is different than the file it is contained in. Read about ES6 modules for details.如果您的接口名称与其包含的文件不同,也会发生此错误。阅读 ES6 模块了解详细信息。 If the SignInComponent was an interface, as was in my case, then如果SignInComponent是一个接口,就像我的情况一样,那么

SignInComponent

should be in a file named SignInComponent.ts .应该在一个名为SignInComponent.ts的文件中。

I got similar issue.我有类似的问题。 The mistake i made was I did not add service in the providers array in app.module.ts.我犯的错误是我没有在 app.module.ts 的 providers 数组中添加服务。 Hope this helps, Thank You.希望这有帮助,谢谢。

我遇到了同样的问题,我刚刚用新端口启动了应用程序,一切看起来都很好。

ng serve --port 4201

我在 app.routing.ts 或 app.module.ts 中的组件名称错误(区分大小写)。

In my module i am exporting classes this way:在我的模块中,我以这种方式导出类:

export { SigninComponent } from './SigninComponent';
export { RegisterComponent } from './RegisterComponent';

This allow me to import multiple classes in file from same module:这允许我从同一个模块导入文件中的多个类:

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

PS: Of course @Fjut answer is correct, but same time it doesn't support multiple imports from same file. PS:当然@Fjut 答案是正确的,但同时它不支持从同一文件中进行多次导入。 I would suggest to use both answers for your needs.我建议根据您的需要使用这两个答案。 But importing from module makes folder structure refactorings more easier.但是从模块导入使文件夹结构重构更容易。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM