AOT Angular 模块

AOT Angular Module

我一直在尝试导出一个简单的 Angular4 模块以在另一个项目中工作至少一个月。看了很多文章,还是不行。 这里 a file containing two folders:

lib -> Containing a simple Angular4 module
demo -> Containing a simple AngularCli project

lib 是 Angular 模块的一个非常简单的实现,demo 正在其根模块中导入该模块。

无论我做什么,演示应用程序都会给我不同类型的错误,指出 lib 不是 Angular 模块。 有时候讲not include decorators,有时候报这个错误:

ERROR in Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 194:50 in the original .ts file), resolving symbol NgModule

我试图瞄准 es5es2015 但没有任何运气。

重现问题的步骤:

  1. 下载 zip 文件并将其解压到一个文件夹中

  2. cd lib文件夹

  3. 运行 npm installyarn install 安装依赖项

  4. 运行 npm run bundleyarn bundle 将库捆绑到 dist

  5. cd demo文件夹

  6. 运行 npm installyarn install

  7. 运行 npm install ../libyarn add file:../lib 安装位于 lib 文件夹

    内的本地 ng-test-lib
  8. 运行 npm startyarn start 启动演示项目

更新:

我在这里添加了一些文件的内容:

lib/src/a-module.ts

import { NgModule } from '@angular/core';
import { ADirective } from './a-directive';

@NgModule({
    declarations: [
        ADirective,
    ],
    exports: [
        ADirective,
    ]
})
export class AModule {}

lib/src/a-directive.ts

import {
    Input,
    OnInit,
    Directive,
 } from '@angular/core';

@Directive({
    selector: '[aDir]',
})
export class ADirective implements OnInit {
    @Input() test: string;

    ngOnInit() {
        console.log(`I'm the directive, and this is test value: ${this.test}`);
    }
}

lib/tsconfig.aot.json

{
    "compilerOptions": {
        "sourceMap": true,
        "inlineSources": true,
        "declaration": true,
        "noImplicitAny": false,
        "skipLibCheck": true,
        "stripInternal": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "suppressImplicitAnyIndexErrors": true,
        "module": "es2015",
        "target": "es5",
        "moduleResolution": "node",
        "lib": [
            "es2015",
            "dom"
        ],
        "outDir": "./dist",
        "rootDir": "./src",
        "baseUrl": ".",
        "types": []
    },
    "files": [
        "./src/a-module.ts"
    ],
    "angularCompilerOptions": {
        "strictMetadataEmit": true,
        "skipTemplateCodegen": true,
        "annotateForClosureCompiler": true,
        "flatModuleOutFile": "index.js",
        "flatModuleId": "index",
        "genDir": "./dist"
    }
}

有人知道这里发生了什么吗?

所以事实证明问题是 symlink-ing 文件夹意味着如果你使用 npm install ../lib 问题就不会发生!我没有尝试,因为我认为它与 yarn add file:../lib 相同,但不。

最后,这是一个与 @angular/compiler 有关的问题,它无法与 symlink 目录一起正常工作。

就是这样。

我希望这能帮助其他人不要像我一样浪费他们的时间。

更新 请注意,如果您使用 npm v5,则会发生此问题,因为它使用 symlink 到 link 本地包。