繁体   English   中英

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

[英]Transpile to ES5 corrupt JS library in Angular 6 project

我正在尝试在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"
    ]
  }
}

会有任何线索吗?

事实证明,按照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的原因。

暂无
暂无

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

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