[英]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.