简体   繁体   English

Angular2上的“请添加@NgModule注释”错误

[英]“Please add a @NgModule annotation” Error on Angular2

I have made a custom angular2(5.0.x) module that looks like this : 我创建了一个自定义angular2(5.0.x)模块,如下所示:

import { GuageService } from './services/guage.service';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GuageComponent } from './guage/guage.component';

@NgModule({
  declarations: [GuageComponent],

  imports: [
    CommonModule
  ],

  providers : [GuageService],
  exports : [GuageComponent]
})
export class GuageModule {}

I use it in my app modules like this: 我在我的app模块中使用它,如下所示:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { DxButtonModule, DxCircularGaugeModule } from 'devextreme-angular';
import { GuageModule } from '@kronsbi/bi-module-template';
import { HttpClientModule } from '@angular/common/http';


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

        @NgModule({
          declarations: [
            AppComponent
          ],
          imports: [
            BrowserModule,
            DxButtonModule,
            DxCircularGaugeModule,
            HttpClientModule,
            GuageModule
          ],
          bootstrap: [AppComponent]
        })
        export class AppModule { }

When I try to start my app I get the following error. 当我尝试启动我的应用程序时,我收到以下错误。

Unexpected value 'GuageModule' imported by the module 'AppModule'. 模块'AppModule'导入的意外值'GuageModule'。 Please add a @NgModule annotation. 请添加@NgModule注释。

UPDATE UPDATE

tsconfig for the main app: 主应用程序的tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

ts config for the GuageModule package: { guageModule包的ts config:{

 "compilerOptions": {
    "baseUrl": ".",
    "declaration": true,
    "stripInternal": true,
    "experimentalDecorators": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "module": "es2015",
    "moduleResolution": "node",
    "paths": {
      "@angular/core": ["node_modules/@angular/core"],
      "rxjs/*": ["node_modules/rxjs/*"]
    },
    "rootDir": ".",
    "outDir": "dist",
    "sourceMap": true,
    "inlineSources": true,
    "target": "es5",
    "skipLibCheck": true,
    "lib": [
      "es2017", 
      "dom"
    ]
  },
  "files": [
    "index.ts"
  ],
  "angularCompilerOptions": {
    "strictMetadataEmit": true
  }
}

If you are using Angular 5.0 you need to add "annotationsAs": "decorators" to the "angularCompilerOptions" for your package. 如果您使用的是Angular 5.0,则需要将"annotationsAs": "decorators"到包的"angularCompilerOptions"中。 Angular 5 introduced new optimizations and by default the decorators are removed on compile because they are not needed at runtime. Angular 5引入了新的优化,默认情况下,在编译时删除了装饰器,因为它们在运行时不需要。 This does not work for packages as you already discovered. 这对您已经发现的包不起作用。 You can read about this in the Angular 5 announcement blog the "Build Optimizer" paragraph mentions this. 你可以在Angular 5公告博客中看到这个,“Build Optimizer”段落提到了这一点。 Version 5.0.0 of Angular Now Available Angular 5.0.0版现已推出

I use this settings in my angular packages: 我在角度包中使用此设置:

"angularCompilerOptions": {
      "skipTemplateCodegen": true,
      "skipMetadataEmit": false,
      "strictMetadataEmit": true,
      "annotationsAs": "decorators"
   }

I had the same problem. 我有同样的问题。 With angular 5+ and angular cli 1.5 it says that your code is not compatible and also your library is scoped package. 对于角度5+和角度cli 1.5,它表示您的代码不兼容,并且您的库也是范围包。 I have managed to fix it with adding 我已设法通过添加来修复它

 export * from './your-path'

In all my files (exporting everything from my library). 在我的所有文件中(从我的库中导出所有内容)。

As i understood its you import as 3 party library You can try to run the application with 据我所知,你导入为3方库您可以尝试运行该应用程序

 ng serve --preserve-symlinks

also add flatModuleId in src/tsconfig.es5.json accordingly: 还相应地在src / tsconfig.es5.json中添加flatModuleId:

"flatModuleId": "@scope/library-name"

Link to github here 链接到github 这里

There is issue on github for more information github上有问题需要更多信息

This is most likely an issue with the way your npm package is being created. 这很可能是您创建npm包的方式的问题。 In your package.json for your GuageModule are you defining a main? 你的GuageModule的package.json中你定义了一个main? This should point to the entry point to your npm package. 这应该指向你的npm包的入口点。 Here is an example of mine. 这是我的一个例子。

"main": "./dist/module.js",

Then if you want typings from GuageModule in your main app you'll need to go to your tsconfig.json and under compilerOptions set declaration to be true to generate the typings files. 然后,如果你想在你的主应用程序中使用GuageModule打字,你需要转到你的tsconfig.json并在compilerOptions下设置declaration为true来生成打字文件。

"compilerOptions": {
  ...
  "declaration": true
  ...
},

Then finally in your package.json you will need to point to the entry point for your typings file. 然后最后在你的package.json你需要指向你的打包文件的入口点。

"types": "./src/app/layout.module.d.ts"

Now you should be able to import your module and have typings on the module that you imported. 现在,您应该可以导入模块并在导入的模块上输入。 Hope this helps! 希望这可以帮助!

请在您的仪表模块的tsconfig.json的compilerOptions中添加“emitDecoratorMetadata”:true

暂无
暂无

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

相关问题 角度6请添加@NgModule注释 - angular 6 Please add a @NgModule annotation 用角度库构建问题的问题。 ng本地服务效果很好错误消息是,请添加一个@NgModule批注 - Problem building poject with angular library. ng serve works fine locally Error message is Please add a @NgModule annotation Unexpected value 'ButtonModule 请添加@NgModule 注解 - Unexpected value 'ButtonModule Please add a @NgModule annotation Angular2服务中的意外值。请添加注释 - Angular2 Unexpected value in Service. Please add annotation 未捕获的错误:模块'AppModule'导入了意外的指令'ProductsComponent'。 请添加@NgModule批注 - Uncaught Error: Unexpected directive 'ProductsComponent' imported by the module 'AppModule'. Please add a @NgModule annotation editor.js:1021未捕获的错误:模块'UserModule'导入了意外的值'UserService'。 请添加@NgModule批注 - compiler.js:1021 Uncaught Error: Unexpected value 'UserService' imported by the module 'UserModule'. Please add a @NgModule annotation 错误:由模块“DynamicTestModule”导入的意外值“DomSanitizer”。 请添加@NgModule 注释 - Error: Unexpected value 'DomSanitizer' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation 错误:在Angular2中找不到“未定义”的NgModule元数据 - Error: No NgModule metadata found for 'undefined' in Angular2 意外值''由模块导入''。 请添加@NgModule注释 - Unexpected value ' ' imported by the module ' '. Please add a @NgModule annotation 模块'...'导入的意外值'...'。 请添加@NgModule注释 - Unexpected value '…' imported by the module '…'. Please add a @NgModule annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM