在Angular 6项目中转换为ES5损坏的JS库

时间:2018-07-25 15:46:35

标签: javascript node.js angular ecmascript-5 tsconfig

我正在尝试在Angular 6项目中使用Dygraph库。它在Chrome / FF中完美运行,但是在IE11中不起作用。我通过npm安装了该库,并从https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/dygraphs

安装了@types

我使用以下命令将库导入到模块中:

import { default as Dygraph } from 'dygraphs';

当我在IE11上打开页面时,加载在以下位置停止:

this.xticks.push({pos, label, has_tick});

错误SCRIPT1003: Expected ':'.。停止加载的块是:

    DygraphLayout.prototype._evaluateLineTicks = function() {
      var i, tick, label, pos, v, has_tick;
      this.xticks = [];
      for (i = 0; i < this.xTicks_.length; i++) {
        tick = this.xTicks_[i];
        label = tick.label;
        has_tick = !('label_v' in tick);
        v = has_tick ? tick.v : tick.label_v;
        pos = this.dygraph_.toPercentXCoord(v);
        if ((pos >= 0.0) && (pos < 1.0)) {
          this.xticks.push({pos, label, has_tick});    <<--- This line stops
        }
      }

      this.yticks = [];
      for (i = 0; i < this.yAxes_.length; i++ ) {
        var axis = this.yAxes_[i];
        for (var j = 0; j < axis.ticks.length; j++) {
          tick = axis.ticks[j];
          label = tick.label;
          has_tick = !('label_v' in tick);
          v = has_tick ? tick.v : tick.label_v;
          pos = this.dygraph_.toPercentYCoord(v, i);
          if ((pos > 0.0) && (pos <= 1.0)) {
            this.yticks.push({axis: i, pos, label, has_tick});
          }
        }
      }
    };

通过查看原始js文件,我们可以观察到在编译过程中已删除了密钥。原始文件中的功能:

DygraphLayout.prototype._evaluateLineTicks = function () {
  var i, tick, label, pos, v, has_tick;
  this.xticks = [];
  for (i = 0; i < this.xTicks_.length; i++) {
    tick = this.xTicks_[i];
    label = tick.label;
    has_tick = !('label_v' in tick);
    v = has_tick ? tick.v : tick.label_v;
    pos = this.dygraph_.toPercentXCoord(v);
    if (pos >= 0.0 && pos < 1.0) {
      this.xticks.push({ pos: pos, label: label, has_tick: has_tick });
    }
  }

  this.yticks = [];
  for (i = 0; i < this.yAxes_.length; i++) {
    var axis = this.yAxes_[i];
    for (var j = 0; j < axis.ticks.length; j++) {
      tick = axis.ticks[j];
      label = tick.label;
      has_tick = !('label_v' in tick);
      v = has_tick ? tick.v : tick.label_v;
      pos = this.dygraph_.toPercentYCoord(v, i);
      if (pos > 0.0 && pos <= 1.0) {
        this.yticks.push({ axis: i, pos: pos, label: label, has_tick: has_tick });
      }
    }
  }
};

我不明白为什么工作库会转换为非工作文件。我的tsconfig.json文件未更改:

{
  "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"
    ]
  }
}

有人有什么线索吗?

1 个答案:

答案 0 :(得分:0)

结果表明,按照jfriend00的建议,配置必须为:

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

lib的作用在文档中并不清楚,但是似乎应该列出所有要编译的源代码的格式。

由于我拥有DOM,ES5和ES6代码,因此我需要它们全部。我使用的模板仅包含DOM / ES6代码,这可能就是为什么lib仅设置为DOM / ES2017的原因。