未捕获的类型错误:无法读取未定义的属性(读取"文档")



我使用nvd3图表(nvd3-1.8.5, d3-3.5.17),从角12升级到角14后面临以下问题。当它在角12时,我没有遇到任何问题。

Uncaught TypeError: Cannot read properties of undefined (reading 'document')
at d3.js:8:26
at 83064 (d3.js:1:2)
at __webpack_require__ (bootstrap:19:1)
at 54058 (index.ts:8:44)
at __webpack_require__ (bootstrap:19:1)
at 84096 (settings.saveview.html:41:141)
at __webpack_require__ (bootstrap:19:1)
at 70299 (UserServiceResolver.ts:18:33)
at __webpack_require__ (bootstrap:19:1)
at 64349 (viewlogmessage.html:65:57)

甚至我尝试了nvd3 1.8.5与d3 4.0.0(与Angular 14)得到以下问题。

core.mjs:6397 ERROR TypeError: nv.dispatch.render_start is not a function
at Object.render (nv.d3.js:93:17)
at nv.addGraph (nv.d3.js:145:12)
at ViewActionsComponent.ts:169:17
at Array.forEach (<anonymous>)
at ViewActionsComponent.onGetMessageSuccessCallback (ViewActionsComponent.ts:140:11)
at SafeSubscriber._next (ViewActionsComponent.ts:83:36)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183:16)
at SafeSubscriber.next (Subscriber.js:122:22)
at Subscriber._next (Subscriber.js:72:26)
at Subscriber.next (Subscriber.js:49:18)

添加包。参考Json配置。

"devDependencies": {
"@angular-devkit/build-angular": "^14.0.2",
"@angular/cli": "^14.0.2",
"@angular/compiler-cli": "^14.0.2",
"@types/ace": "0.0.32",
"@types/angular": "1.6.21",
"@types/argparse": "1.0.30",
"@types/ckeditor": "0.0.37",
"@types/core-js": "0.9.36",
"@types/d3": "^3.5.47",
"@types/d3pie": "^0.1.5",
"@types/glob": "5.0.30",
"@types/jasmine": "~3.6.0",
"@types/jasminewd2": "2.0.2",
"@types/jquery": "3.5.6",
"@types/lodash": "^4.14.182",
"@types/minimatch": "2.0.29",
"@types/node": "^12.20.50",
"@types/nvd3": "^1.8.42",
"@types/shelljs": "0.7.0",
"access-sniff": "2.4.61",
"angular2-template-loader": "0.6.2",
"argparse": "1.0.9",
"awesome-typescript-loader": "3.1.2",
"babel-preset-es2015": "6.18.0",
"bootstrap": "3.4.1",
"codelyzer": "^6.0.0",
"copy-webpack-plugin": "4.0.1",
"copyfiles": "1.0.0",
"cross-env": "^5.0.5",
"css-loader": "0.27.3",
"d3": "4.0.0",
"d3pie": "0.1.5",
"extract-text-webpack-plugin": "3.0.2",
"file-appender": "^1.1.0",
"fs": "0.0.1-security",
"fs-extra": "5.0.0",
"glob": "7.1.1",
"glob-concat": "^1.0.1",
"gnomon": "1.5.0",
"increase-memory-limit": "^1.0.3",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"js-yaml": "^3.13.1",
"less": "2.7.1",
"less-loader": "4.0.5",
"less-plugin-clean-css": "1.5.1",
"lite-server": "^2.6.1",
"mkdirp": "^0.5.6",
"ncp": "2.0.0",
"npm-build-tools": "^2.2.5",
"nvd3": "1.8.5",
"perfect-scrollbar": "0.8.1",
"protractor": "~7.0.0",
"raw-loader": "0.5.1",
"remap-istanbul": "0.7.0",
"rimraf": "2.5.4",
"rollup-loader": "^0.3.0",
"shelljs": "0.7.5",
"style-loader": "0.16.1",
"systemjs": "^6.12.1",
"time-grunt": "^1.4.0",
"to-string-loader": "1.1.5",
"ts-helpers": "1.1.1",
"ts-node": "~3.2.0",
"tslint": "~6.1.0",
"typedoc": "0.5.7",
"typescript": "~4.6.4",
"uglify-js": "3.0.26",
"webpack-dev-server": "3.11.2"
},
"dependencies": {
"@angular/animations": "^14.0.2",
"@angular/common": "^14.0.2",
"@angular/compiler": "^14.0.2",
"@angular/core": "^14.0.2",
"@angular/forms": "^14.0.2",
"@angular/localize": "^14.0.2",
"@angular/platform-browser": "^14.0.2",
"@angular/platform-browser-dynamic": "^14.0.2",
"@angular/platform-server": "^14.0.2",
"@angular/router": "^14.0.2",
"@ng-bootstrap/ng-bootstrap": "^9.1.3",
"@ngx-translate/core": "^13.0.0",
"@ngx-translate/http-loader": "^6.0.0",
"@types/bootstrap": "^5.1.10",
"ace-builds": "^1.4.14",
"ag-grid-angular": "^26.1.0",
"ag-grid-community": "^26.1.0",
"circular-dependency-plugin": "^5.2.2",
"commonjs-require": "1.4.6",
"core-js": "^2.6.7",
"es6-collections": "0.5.6",
"es6-promise": "3.3.1",
"es6-shim": "0.34.1",
"fs": "0.0.1-security",
"jquery": "^3.6.0",
"moment": "^2.29.3",
"pdfjs-dist": "2.0.489",
"plotly.js": "2.12.1",
"plugin-typescript": "4.0.17",
"reflect-metadata": "0.1.9",
"rxjs": "^6.6.0",
"rxjs-compat": "^6.6.7",
"rxjs-tslint": "^0.1.8",
"ts-smart-logger": "0.0.4",
"tslib": "^2.4.0",
"webpack": "^5.73.0",
"zone.js": "^0.11.5"
}

你知道如何修复这个错误吗?

我知道这可能不是一个正确的修复,但是

  • remove all import d3
  • 在index.html文件中使用CDN链接:
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.7/d3.min.js"
integrity="sha512-MbgT+83UxCH6sDPhdRyAqCvG+93y1xm6SIC/1T15gWsbkC2bjGqHVWepfhPMocnEOhgXBtZCOxHmwq0XPsqSGQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>

我也遇到过同样的问题,所以我采取了行动。

关于d3 js类型,你可以把它作为依赖项。

最新更新