升级到 Angular 8 后 d3.js 出错
Error in d3.js after upgrading to Angular 8
我已将我的 Angular 4 应用更新为 Angular 8。应用程序在开发构建中运行良好,但在生产构建中中断。加载应用程序时出现以下错误。
Uncaught TypeError: Cannot read property 'document' of undefined
at d3.js:8
at Object../node_modules/d3/d3.js (d3.js:9554)
at webpack_require (bootstrap:78)
at Module../src/app/pages/home/dashboard/map/map.component.ts (main-es2015.js:9841)
at webpack_require (bootstrap:78)
at Module../src/app/pages/home/home.routes.ts (home.routes.ts:2)
at webpack_require (bootstrap:78)
at Module../src/app/pages/home/home.module.ts (home.event.bus.ts:5)
at webpack_require (bootstrap:78)
我的 tsconfig.json 文件如下所示。
{
"compileOnSave": false,
"compilerOptions": {
"importHelpers": true,
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
],
"module": "esnext",
"paths": {
"core-js/es7/reflect": ["../node_modules/core-js/proposals/reflect-
metadata"],
"core-js/es6/*": ["../node_modules/core-js/es/*"]
}
}
}
我尝试将目标从 es2015 更改为 es5 但没有成功,并在 vendor.js 中给我一个错误
任何帮助,将不胜感激。谢谢
我通过将 tsconfig.json 文件中的目标从 es2015 更改为 es5[ 来解决这个问题=18=]。 d3 库目前与 es2015 不兼容。
而 return 中的那个在 d3.js 中抛出错误,因为使用了 UTF-8 符号,例如 π。要解决此问题,您可以在 HTML 主机文档上指定 UTF-8 字符集。
<meta http-equiv="content-type" content="text/html; charset=UTF8">
Angular 8 决定在目标 es2015+ 时将 type="module"
放到 index.html 中。在我的示例中,我成功地将 es2017 与 Angular 7 一起使用,当然包括 d3(通过 plotly.js)。除了修改 dist/index.html 后,我还没有找到任何解决方案。对我来说,这是一个完美的工作解决方案,因为生成的代码工作正常。
要执行相同的操作,请先安装 https://www.npmjs.com/package/@angular-builders/custom-webpack 并按照他们的描述进行配置。创建 extra-webpack.config.js 后,您将以下代码放入此文件:
class FixIndex {
constructor(path) {
this.path = path;
}
done() {
const path = __dirname + '/dist/' + this.path;
if (!fs.existsSync(path)) {
console.log('path not found', path);
return;
}
let file = fs.readFileSync(path, 'utf8');
file = file.replace(/type="module"/g, '');
fs.writeFileSync(path, file);
}
apply(compiler) {
compiler.hooks.done.tap(
'FixIndex',
() => {
setTimeout(() => {
this.done();
}, 1);
}
);
}
}
module.exports = {
plugins: [
new FixIndex('deepkit/index.html'),
],
}
我已将我的 Angular 4 应用更新为 Angular 8。应用程序在开发构建中运行良好,但在生产构建中中断。加载应用程序时出现以下错误。
Uncaught TypeError: Cannot read property 'document' of undefined at d3.js:8 at Object../node_modules/d3/d3.js (d3.js:9554) at webpack_require (bootstrap:78) at Module../src/app/pages/home/dashboard/map/map.component.ts (main-es2015.js:9841) at webpack_require (bootstrap:78) at Module../src/app/pages/home/home.routes.ts (home.routes.ts:2) at webpack_require (bootstrap:78) at Module../src/app/pages/home/home.module.ts (home.event.bus.ts:5) at webpack_require (bootstrap:78)
我的 tsconfig.json 文件如下所示。
{
"compileOnSave": false,
"compilerOptions": {
"importHelpers": true,
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
],
"module": "esnext",
"paths": {
"core-js/es7/reflect": ["../node_modules/core-js/proposals/reflect-
metadata"],
"core-js/es6/*": ["../node_modules/core-js/es/*"]
}
}
}
我尝试将目标从 es2015 更改为 es5 但没有成功,并在 vendor.js 中给我一个错误 任何帮助,将不胜感激。谢谢
我通过将 tsconfig.json 文件中的目标从 es2015 更改为 es5[ 来解决这个问题=18=]。 d3 库目前与 es2015 不兼容。
而 return 中的那个在 d3.js 中抛出错误,因为使用了 UTF-8 符号,例如 π。要解决此问题,您可以在 HTML 主机文档上指定 UTF-8 字符集。
<meta http-equiv="content-type" content="text/html; charset=UTF8">
Angular 8 决定在目标 es2015+ 时将 type="module"
放到 index.html 中。在我的示例中,我成功地将 es2017 与 Angular 7 一起使用,当然包括 d3(通过 plotly.js)。除了修改 dist/index.html 后,我还没有找到任何解决方案。对我来说,这是一个完美的工作解决方案,因为生成的代码工作正常。
要执行相同的操作,请先安装 https://www.npmjs.com/package/@angular-builders/custom-webpack 并按照他们的描述进行配置。创建 extra-webpack.config.js 后,您将以下代码放入此文件:
class FixIndex {
constructor(path) {
this.path = path;
}
done() {
const path = __dirname + '/dist/' + this.path;
if (!fs.existsSync(path)) {
console.log('path not found', path);
return;
}
let file = fs.readFileSync(path, 'utf8');
file = file.replace(/type="module"/g, '');
fs.writeFileSync(path, file);
}
apply(compiler) {
compiler.hooks.done.tap(
'FixIndex',
() => {
setTimeout(() => {
this.done();
}, 1);
}
);
}
}
module.exports = {
plugins: [
new FixIndex('deepkit/index.html'),
],
}