无法读取未定义的属性‘节点’

无法读取未定义的属性‘节点’

本文介绍了将 d3-tip 与 npm 一起使用:“未捕获的类型错误:无法读取未定义的属性‘节点’"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已经安装了 d3": "^3.5.17""d3-tip": "^0.7.1" 使用 npm (d3-tip 文档.然后在我的 index.js 文件中我有这段代码:

I've installed d3": "^3.5.17" and "d3-tip": "^0.7.1" using npm (d3-tip documentation). Then in my index.js file I have this code:

var d3 = require('d3');
var d3tip = require('d3-tip')(d3);
console.log('d3 version', d3.version);
var tip = d3tip().attr('class', 'd3-tip').html(function(d) { return "hello world"; })

但是当我使用 browserify 构建索引文件并将其加载到浏览器中时,我看到 var tip 行出现错误:

But when I build the index file with browserify and load it in the browser, I see an error from the var tip line:

index.js:247 Uncaught TypeError: Cannot read property 'node' of undefined

这是来自 d3-tip 源代码中的这个函数:

This is coming from this function in the d3-tip source code:

function getSVGNode(el) {
  el = el.node()
  if(el.tagName.toLowerCase() === 'svg')
    return el
  return el.ownerSVGElement
}

看起来这个函数是期望一个节点被传递给它?但这是从哪里来的?

It looks like this function is expecting a node to be passed to it? But where would this come from?

构建本身不会引发任何错误,我认为我正确地需要 d3-tip,根据这个问题.控制台语句按预期显示 d3 版本 3.5.17.

The build itself does not throw any errors, and I think I'm requiring d3-tip correctly, as per this question. The console statement shows d3 version 3.5.17, as expected.

更新:这是我的 package.json 文件:

UPDATE: Here's my package.json file:

{
  "name": "myapp",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "watchify index.js -o main.js --debug --verbose",
    "build": "browserify index.js | uglifyjs > main.min.js"
  },
  "dependencies": {
    "d3": "^3.5.17",
    "d3-tip": "^0.7.1",
    "datatables.net": "^1.10.12",
    "datatables.net-bs": "^1.10.12"
  },
  "devDependencies": {
    "uglifyjs": "^2.4.10",
    "watchify": "^3.2.1"
  }
}

我用 npm install 安装了这些文件.

And I installed the files with npm install.

推荐答案

发生这种情况是因为我试图将最新版本的 d3-tip(需要 D3 的 v4)与 D3 的 v3 一起使用.

This was happening because I was trying to use the latest version of d3-tip (which requires v4 of D3) with v3 of D3.

恢复到旧版本的两个固定的东西:

Reverting to older versions of both fixed things:

"d3": "^3.5.17",
"d3-tip": "^0.6.7"

这篇关于将 d3-tip 与 npm 一起使用:“未捕获的类型错误:无法读取未定义的属性‘节点’"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:51