【发布时间】:2018-08-01 15:14:01
【问题描述】:
更新:我的领导能够解决这个问题。请参阅下面的答案,我希望这至少对某些人有所帮助
以下代码会引发异常,但请注意,当我不导入/使用 d3-selection 时,整个应用程序运行时不会出现错误。一旦我从“d3-selection”导入选择,我就会收到我在标题中提到的错误。
import { Component, ElementRef, ViewChild } from '@angular/core';
import { select } from 'd3-selection';
@Component({
selector: 'pie',
template: `<ng-content></ng-content>`
})
export class PieChartComponent {
@ViewChild('containerPieChart')
private element: ElementRef;
constructor() {
select(this.element.nativeElement);
}
}
我在这里检查了可能的骗子,但没有一个适用于我,所以我在这里。
从 TypeScript 捆绑/导入的代码是:
function(name) {
return select(creator(name).call(document.documentElement));
}
我知道这在 JS 中是无效的,因为函数必须有名称,或者是 IIFE 才能省略名称。或对象声明。那么,为什么 d3 会转译成无效的 JS?
编辑:我正在使用带有以下代码的 rollup.config.dev.js:
import bundles from './bundles.json';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import scss from 'rollup-plugin-scss';
import sourcemaps from 'rollup-plugin-sourcemaps';
const
DEV_DIRECTORY = `dev`
, MODULE_NAME_PATH = 'AST.app'
;
function createDevBundle(bundle) {
let bundleDirectory = `${DEV_DIRECTORY}/${bundle.name}`;
return {
input: bundle.input,
name: `${MODULE_NAME_PATH}.${bundle.name}`,
output: {
file: `${bundleDirectory}/${bundle.name}.js`,
format: 'umd'
},
exports: 'default',
plugins: [
resolve({
jsnext: true,
module: true
}),
commonjs(),
sourcemaps(),
scss({
output: `${bundleDirectory}/${bundle.name}.css`,
includePaths: ['../global/scss/base']
})
],
onwarn: function(warning) {
// Recommended warning suppression by angular team.
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
// eslint-disable-next-line no-console
console.error(warning.message);
},
treeshake: false,
sourcemap: true
};
}
export default bundles.map(createDevBundle);
【问题讨论】:
-
在没有类型定义的情况下迁移到 D3V3 作为一种解决方法,直到我弄清楚这个坏男孩。
标签: javascript d3.js angular5