【发布时间】:2019-10-17 10:01:54
【问题描述】:
我正在尝试将我的 Angular 6 应用程序升级到 Angular 8。我的代码可以编译,但我立即收到运行时错误“d3.js:8 Uncaught TypeError: Cannot read property 'document' of undefined”。
d3.js 中失败的行是var d3_document = this.document;。这让我相信 Angular 8 在严格模式下运行 d3.js。我有最新版本的d3节点模块("d3": "3.5.17"),显然不支持严格模式;我的理解是“this”应该引用窗口对象,但在严格模式下不起作用。
我知道 Angular 8 现在使用 dart-sass 而不是 node-sass,据说后者更严格。我确实尝试安装 node-sass 来使用它而不是 dart-sass(按照升级文档的建议),但我很确定这与 sass 无关。
我会注意到我的一些其他包需要更新,因为它们依赖于 Angular 6 的包,但我看不出这会如何影响他的 d3 错误。
我也尝试在我的 tsconfig.json 文件中明确说出 "noImplicitUseStrict": false,,但收到了同样的错误。我也尝试过"noImplicitUseStrict": true,,但没有成功。
我参考了这篇解决相同错误的堆栈溢出帖子:D3.js : Uncaught TypeError: Cannot read property 'document' of undefined,以及参考的解决方案: https://stackoverflow.com/questions/33821312/how-to-remove-global-use-strict-added-by-babel;但是我很难将这些应用于我的情况,因为我正在处理一个 Angular 项目并且不确定 babel 是否适用或如何修改 babel 选项。
完全错误:
d3.js: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:83)
at Module../dist/core-services/fesm2015/core-services.js (core-services.js:1)
at __webpack_require__ (bootstrap:83)
at Module../src/app/app.component.ts (main-es2015.js:22262)
at __webpack_require__ (bootstrap:83)
at Module../src/app/app.module.ts (app.component.ts:21)
at __webpack_require__ (bootstrap:83)
at Module../src/main.ts (main.ts:1)
(anonymous) @ d3.js:8
./node_modules/d3/d3.js @ d3.js:9554
__webpack_require__ @ bootstrap:83
./dist/core-services/fesm2015/core-services.js @ core-services.js:1
__webpack_require__ @ bootstrap:83
./src/app/app.component.ts @ main-es2015.js:22262
__webpack_require__ @ bootstrap:83
./src/app/app.module.ts @ app.component.ts:21
__webpack_require__ @ bootstrap:83
./src/main.ts @ main.ts:1
__webpack_require__ @ bootstrap:83
0 @ main.ts:17
__webpack_require__ @ bootstrap:83
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main-es2015.js:1
预计没有错误。有没有办法指定我不希望这个节点模块在严格模式下运行?
【问题讨论】:
-
嗨@Reactgular,我确实看到了那篇文章,并试图将其应用于我的情况。我不知道如何修改 Angular 项目中的 babel 选项。
标签: javascript angular d3.js node-modules angular8