简体   繁体   English

我的库模块无法识别为NgModule

[英]My library module is not recognize as an NgModule

My AccordionModule in my angular 2 library seems to be not a NgModule during the first compilation by angular-cli but he reload again alone after the error and then he can compile and the module is recognize. angular-cli的第一次编译过程中,我的angular 2库中的AccordionModule似乎不是NgModule,但是在错误之后他再次单独重新加载,然后他可以编译并且模块被识别。

  Time: 7903ms
    chunk    {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 158 kB {4} [initial] [rendered]
    chunk    {1} main.bundle.js, main.bundle.js.map (main) 11.4 kB {3} [initial] [rendered]
    chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 9.77 kB {4} [initial] [rendered]
    chunk    {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.6 MB [initial] [rendered]
    chunk    {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]

    ERROR in AccordionModule is not an NgModule
    webpack: Failed to compile.
    webpack: Compiling...
    Hash: 422f957171e71c80b1cf                                                         
    Time: 1999ms
    chunk    {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 158 kB {4} [initial]
    chunk    {1} main.bundle.js, main.bundle.js.map (main) 11.4 kB {3} [initial]
    chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 9.77 kB {4} [initial]
    chunk    {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.6 MB [initial]
    chunk    {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry]
    webpack: Compiled successfully.

I tried to upgrade typescript to the version 2.0.10 and ̀ 2.0.3 but that change nothing. 我试图升级typescript到版本2.0.102.0.3 ,但这种改变什么。

Here is my dependencies : 这是我的依赖项:

 "peerDependencies": {
        "@angular/common": "^2.4.0",
        "@angular/compiler": "^2.4.0",
        "@angular/core": "^2.4.0",
        "@angular/platform-browser": "^2.4.0",
        "core-js": "2.4.1",
        "typescript": "2.0.10",
        "zone.js": "^0.7.6"
    },
    "devDependencies": {
        "@angular/compiler-cli": "^2.4.0",
        "@angular/platform-server": "^2.4.0",
        "@types/jasmine": "2.5.38",
        "@types/node": "6.0.60",
        "rimraf": "^2.5.4"
    }

And my tsconfig : 我的tsconfig:

{
    "compilerOptions": {
        "baseUrl": "",
        "target": "es5",
        "module": "es2015",
        "moduleResolution": "node",
        "declaration": true,
        "noImplicitAny": false,
        "strictNullChecks": true,
        "sourceMap": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "outDir": "./dist",
        "rootDir": "./src",
        "skipLibCheck": true,
        "stripInternal": true,
        "typeRoots": ["./node_modules/@types"],
        "types": [
            "node"
        ],
        "lib": [
            "dom",
            "es2015"
        ],
        "paths": {
            "@angular/core": ["node_modules/@angular/core"]
        }
    },
    "compileOnSave": false,
    "buildOnSave": false,
    "exclude": [
        "node_modules",
        "dist",
        "tests",
        "**/*.ngfactory.ts",
        "**/*.shim.ts"
    ],
    "angularCompilerOptions": {
        "genDir": "dist",
        "entryModule": "src/lib/module#AccordionModule",
        "skipTemplateCodegen": true,
        "strictMetadataEmit": true
    }
}

Compile your package in AOT using the ngc compiler instead of the tsc compiler. 使用ngc编译器而不是tsc编译器在AOT中编译包。 You can point it to the same tsconfig file. 您可以将其指向同一个tsconfig文件。

npm install @angular/compiler-cli @angular/platform-server --save
node_modules/.bin/ngc -p tsconfig-aot.json

//Windows users should surround the ngc command in double quotes:
"node_modules/.bin/ngc" -p tsconfig.json

More details on this article https://angular.io/docs/ts/latest/cookbook/aot-compiler.html#!#compile 关于这篇文章的更多细节https://angular.io/docs/ts/latest/cookbook/aot-compiler.html#!#compile

The ngc compiler will output extra metadata that Angular uses to recognize your module. ngc编译器将输出Angular用于识别模块的额外元数据。

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

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