这个错误通常发生在使用箭头函数时,由于箭头函数没有自己的上下文,所以无法正确地绑定this关键字。您可以尝试以下解决方法:
d3.select("circle")
.on("mouseover", function() {
this.setAttribute("fill", "red");
});
const handleMouseOver = () => {
this.setAttribute("fill", "red");
};
d3.select("circle")
.on("mouseover", handleMouseOver);
d3.select("circle")
.on("mouseover", () => {
this.setAttribute("fill", "red");
}).bind(this);
请记住,以上解决方法适用于Angular 6和d3 v5。确保在使用setAttribute方法之前,确保this是指向您期望的元素。