我已将我的 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 解决了这个问题。 d3 库目前与 es2015 不兼容。
由于使用了 π 等 UTF-8 符号,这反过来会在 d3.js 中引发错误。要解决此问题,您可以在 HTML 主机文档上指定 UTF-8 字符集。
<meta http-equiv="content-type" content="text/html; charset=UTF8">
关于angular - 升级到 Angular 8 后 d3.js 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141011/