我使用webpack和TypeScript,看起来d3-tip不能使用webpack。我在鼠标悬停事件上遇到错误 "Uncaught TypeError: Cannot read property 'target' of null"
。d3-tip不能与webpack一起使用
发生此错误是因为d3-tip模块中的d3.event为null。
我包括模块如下:
const d3: any = require("d3");
d3.tip = require("d3-tip");
但我想这D3存在和D3 D3的尖端模块中是不同的,这是问题的根源,但我不知道如何解决它。在D3-针尖模块有:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module with d3 as a dependency.
define(['d3'], factory)
} else if (typeof module === 'object' && module.exports) {
// CommonJS
var d3 = require('d3')
module.exports = factory(d3)
} else {
// Browser global.
root.d3.tip = factory(root.d3)
}
}(this, function (d3) {
...
并将其编译通过的WebPack到
function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;// d3.tip
// Copyright (c) 2013 Justin Palmer
//
// Tooltips for d3.js SVG visualizations
(function (root, factory) {
if (true) {
// AMD. Register as an anonymous module with d3 as a dependency.
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [__webpack_require__(465)], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory), __WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ? (__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__))
} else if (typeof module === 'object' && module.exports) {
// CommonJS
var d3 = require('d3')
module.exports = factory(d3)
} else {
// Browser global.
root.d3.tip = factory(root.d3)
}
}(this, function (d3) {
...
,并很明显,AMD正在使用。如果我能得到d3-tip的工厂,我会解决这个问题。
您是否使用D3版本3或4?我遇到了一个类似的问题,使用带有webpack的d3版本3。 – softweave