【问题标题】:Compiling Angular 2 as commonjs将 Angular 2 编译为 commonjs
【发布时间】:2017-05-23 21:38:27
【问题描述】:

我想将 Angular 2 项目编译为 commonjs 模块。

我有这个tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "mapRoot": "./",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "../dist/lib",
    "sourceMap": true,
    "target": "es5",
    "removeComments": true
  }
}

我使用ntsc,但出现以下错误:

node_modules/@angular/common/src/directives/ng_class.d.ts(46,34): error TS2304: Cannot find name 'Set'.
node_modules/@angular/common/src/pipes/async_pipe.d.ts(44,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/compiler/src/aot/compiler.d.ts(32,38): error TS2304: Cannot find name 'Promise'.
node_modules/@angular/compiler/src/aot/compiler.d.ts(44,32): error TS2304: Cannot find name 'Map'.

我有npm版本4.0.5nodev7.3.0ntscVersion 2.1.0

【问题讨论】:

    标签: angular typescript commonjs


    【解决方案1】:

    要转译为 es5,您必须在主 ts 文件中添加以下参考:

    ///<reference path="node_modules/angular2/typings/browser.d.ts"/>

    【讨论】:

    • 我在 Angular-CLI 生成的项目中遇到了类似的问题。我正在使用 Eclipse 和来自Angelo Zerr 的插件。 “ng build”和“ng serve”命令完美运行,但 tsc 在 node_modules 下显示错误。您的解决方案中的路径 (node_modules/angular2/typings/browser.d.ts) 不存在,它是 angular2 beta 的旧路径。在当前的 Angular 版本中如何解决这个问题?
    【解决方案2】:

    你想要target: "es6",那些缺失的类在 ECMAScript 5 中不可用

    【讨论】:

    • 但是如果我使用"es6"作为target,我也必须从ES6编译到ES5
    • 我不知道如何将 es 6 代码转换为 es 5 并且仍然可以工作。但是如果你想在 es5 代码中使用 es6 特性,你应该看看 babel 让你这样做
    • 我关注了这篇文章:blog.angular-university.io/…
    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2012-09-02
    • 2017-08-06
    相关资源
    最近更新 更多