错误:模块导入的意外值'undefined'

sur*_*sea 78 angular

迁移到NgModule后我收到此错误,错误没有太大帮助,请问有什么建议吗?

Error: Error: Unexpected value 'undefined' imported by the module 'AppModule'
        at new BaseException (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:5116:27)
        at eval (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:13231:35)
        at Array.forEach (native)
        at CompileMetadataResolver.getNgModuleMetadata (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:13215:48)
        at RuntimeCompiler._compileComponents (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:15845:51)
        at RuntimeCompiler._compileModuleAndComponents (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:15769:41)
        at RuntimeCompiler.compileModuleAsync (http://localhost:5555/node_modules/@angular/compiler/bundles/compiler.umd.js:15746:25)
        at PlatformRef_._bootstrapModuleWithZone (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:9991:29)
        at PlatformRef_.bootstrapModule (http://localhost:5555/node_modules/@angular/core/bundles/core.umd.js:9984:25)
        at Object.eval (http://localhost:5555/app/main.js:8:53)
    Evaluating http://localhost:5555/app/main.js
    Error loading http://localhost:5555/app/main.js "Report this error at https://github.com/mgechev/angular2-seed/issues"(anonymous function) @ contracts:142ZoneDelegate.invoke @ zone.js?1472711930202:332Zone.run @ zone.js?1472711930202:225(anonymous function) @ zone.js?1472711930202:586ZoneDelegate.invokeTask @ zone.js?1472711930202:365Zone.runTask @ zone.js?1472711930202:265drainMicroTaskQueue @ zone.js?1472711930202:491ZoneTask.invoke @ zone.js?1472711930202:435
Run Code Online (Sandbox Code Playgroud)

app.module.ts:

    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { APP_BASE_HREF } from '@angular/common';
    import { RouterModule } from '@angular/router';
    import { HttpModule } from '@angular/http';
    import { AppComponent } from './app.component';
    import { routes } from './app.routes';

    import { provide } from '@angular/core';

    //dgf ng2-translate
    import { TRANSLATE_PROVIDERS, TranslateLoader, TranslateStaticLoader, MissingTranslationHandler } from 'ng2-translate/ng2-translate';
    import { HTTP_PROVIDERS, Http } from '@angular/http';
    import { FormsModule,ReactiveFormsModule } from '@angular/forms';
    import { TranslationNotFoundHandler } from './shared/common/TranslationNotFoundHandler';
    //dgf ng2-translate END

    import {CalendarModule,DataTableModule,DialogModule,PanelModule} from 'primeng/primeng';

    import {TranslateModule} from 'ng2-translate/ng2-translate';

    import { AuthGuard,AppConfigService,AppConfig,DateHelper,ThemeComponent,ToolbarComponent, RemoveHostTagDirective } from './index';
    import { HomeComponent,MessagesExampleComponent,PrimeNgHomeComponent,CalendarComponent,Ng2BootstrapExamplesComponent,DatepickerDemoComponent,UserListComponent,UserEditComponent,ContractListComponent,AboutComponent } from './index';


@NgModule({
  imports: [BrowserModule, HttpModule, RouterModule.forRoot(routes), /* AboutModule, HomeModule, SharedModule.forRoot()*/
          FormsModule,
          ReactiveFormsModule,
          //third-party
          ,TranslateModule.forRoot() //,
          //third-party PRIMENG
          ,CalendarModule,DataTableModule,DialogModule,PanelModule
  ],
  declarations: [
    AppComponent,ThemeComponent, ToolbarComponent, RemoveHostTagDirective,
    HomeComponent,MessagesExampleComponent,PrimeNgHomeComponent,CalendarComponent,Ng2BootstrapExamplesComponent,DatepickerDemoComponent,UserListComponent,UserEditComponent,ContractListComponent,AboutComponent
  ],
  providers: [{
      provide: APP_BASE_HREF,
      useValue: '<%= APP_BASE %>'
    },
    FormsModule,
    ReactiveFormsModule,
    provide(TranslateLoader, { //DGF ng2-translate
          useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
          deps: [Http]
      }),
    provide(MissingTranslationHandler, { useClass: TranslationNotFoundHandler }), //DGF ng2-translate

    AuthGuard,AppConfigService,AppConfig,
    DateHelper
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

Jal*_*aer 175

对于任何面临同样错误的人,我的情况是我在导入部分有双逗号

imports: [
      BrowserModule,
      HttpModule,
      FormsModule,
      RouterModule.forRoot(appRoutes), , // <-- this was the error
      // .... 
],
Run Code Online (Sandbox Code Playgroud)

  • 2年后,你节省了我的时间!谢谢 (4认同)
  • 对我来说这是一个点 (.) (2认同)

Tuo*_* Le 75

确保模块不会相互导入.所以,不应该

在模块A中: imports[ModuleB]

在模块B中: imports[ModuleA]


Ign*_*rew 51

这可能是由多种情况引起的

  1. 缺少逗号
imports: [
    BrowserModule
   ,routing <= Missing Comma
   ,FeatureComponentsModule
  ],
Run Code Online (Sandbox Code Playgroud)
  1. 双逗号
imports: [
    BrowserModule, 
   ,routing <=Double Comma
   ,FeatureComponentsModule
  ],
Run Code Online (Sandbox Code Playgroud)
  1. 从模块中导出任何内容
  2. 语法错误
  3. 错字错误
  4. 对象,数组中的分号
  5. 导入语句不正确

  • 很好的总结.如果Angular可以在某处以及模块加载失败的方式输入一些信息,那将会很棒.就目前而言,这是调试最繁琐的事情之一.不确定我是否理解"缺少逗号"位,那应该编译好吗? (3认同)

Sem*_*jić 23

上述解决方案都不适用于我,而只是停止并再次运行"ng服务".

  • 哈哈,对我来说也是.当你乱用路由时,有时Angular需要重启.在模块之间移动重要的核心导入或导出也是如此. (3认同)

Hel*_*ate 21

我有这个错误,因为index.ts我的应用程序的根目录中有一个导出我的文件app.component.ts.所以我想我可以做到以下几点:

import { AppComponent } from './';
Run Code Online (Sandbox Code Playgroud)

这工作并没有给我任何红色波浪线,甚至intellisense在你开始输入它时会调出AppComponent.但是来发现它导致了这个错误.我把它改成后:

import { AppComponent } from './app.component';
Run Code Online (Sandbox Code Playgroud)

错误消失了.


Gen*_*sov 19

我有同样的问题.在我的情况下,原因是额外的逗号.

在此输入图像描述


Tuo*_* Le 7

确保不要导入这样的模块/组件:

import { YOUR_COMPONENT } from './';
Run Code Online (Sandbox Code Playgroud)

但它应该是

import { YOUR_COMPONENT } from './YOUR_COMPONENT.ts';
Run Code Online (Sandbox Code Playgroud)


小智 7

本期两个模块的循环依赖

模块1:导入模块2
模块2:导入模块1


Nay*_*fin 6

我正在构建一个组件库,并开始在我导入所述库的应用程序中收到此错误。在运行ng build --prodng serve --aot在应用程序中我会得到:

Unexpected value 'undefined' imported by the module '?m in node_modules/<library-name>/<library-name>.d.ts'

但是在使用ng serve或测试库本身中的模块时没有错误,即使在--prod.

结果我也被智能感知误导了。对于我的一些模块,我导入了一个姊妹模块作为

import { DesignModule } from '../../design';

代替

import { DesignModule } from '../../design/design.module';

除了我描述的那个之外,它在所有版本中都运行良好。

这很难确定,我很幸运它并没有比我花费更长的时间。希望这有助于某人。


小智 6

尝试编译 Angular 5 应用程序时出现相同的异常。

Unexpected value 'undefined' imported by the module 'DemoAppModule'
Run Code Online (Sandbox Code Playgroud)

就我而言,结果证明这是我通过使用工具madge发现的循环依赖。通过运行找到包含循环依赖的文件

npx madge --circular --extensions ts src/
Run Code Online (Sandbox Code Playgroud)


小智 6

就我而言,我将 ngrx 放入provideMockStore()测试模块的“导入”而不是“提供者”数组中。


sur*_*sea 3

只需将提供程序放入 forRoot 即可:https ://github.com/ocombe/ng2-translate

@NgModule({
  imports: [BrowserModule, HttpModule, RouterModule.forRoot(routes), /* AboutModule, HomeModule, SharedModule.forRoot()*/
          FormsModule,
          ReactiveFormsModule,
          //third-party
          TranslateModule.forRoot({
            provide: TranslateLoader,
            useFactory: (http: Http) => new TranslateStaticLoader(http, '/assets/i18n', '.json'),
            deps: [Http]
          })
          //third-party PRIMENG
          ,CalendarModule,DataTableModule,DialogModule,PanelModule
  ],
  declarations: [
    AppComponent,ThemeComponent, ToolbarComponent, RemoveHostTagDirective,
    HomeComponent,MessagesExampleComponent,PrimeNgHomeComponent,CalendarComponent,Ng2BootstrapExamplesComponent,DatepickerDemoComponent,UserListComponent,UserEditComponent,ContractListComponent,AboutComponent
  ],
  providers: [
    {
      provide: APP_BASE_HREF,
      useValue: '<%= APP_BASE %>'
    },
    // FormsModule,
    ReactiveFormsModule,
    { provide : MissingTranslationHandler, useClass: TranslationNotFoundHandler},
    AuthGuard,AppConfigService,AppConfig,
    DateHelper
  ],
  bootstrap: [AppComponent]
})

export class AppModule { }
Run Code Online (Sandbox Code Playgroud)