typescript - 类型“HierarchyNode”上不存在属性“x”'
问题描述
import React, { useRef, useEffect } from "react";
import { select, hierarchy, tree } from "d3";
function ThreeChart({ data }: any) {
const svgRef = useRef(null);
const wrapperRef = useRef(null);
useEffect(() => {
const svg = select(svgRef.current);
const root = hierarchy(data);
svg
.selectAll(".node")
.data(root.descendants())
.join("circle")
.attr("class", "node")
.attr("fill", "black").attr("cx", node => node.x) // Property 'x' does not exist
.attr("r", 4)
}, [data]);
return (
<div ref={wrapperRef} style={{ marginBottom: "2rem" }}>
<svg ref={svgRef}></svg>
</div>
);
}
export default ThreeChart;
我认为这非常简单明了,但是因为我是打字稿的新手,所以我似乎无法弄清楚,我应该在什么地方或在哪里声明我的类型,我已经尝试过了,但它不起作用:
interface DataProps {
children: DataProps[];
depth: number;
height: number;
x: number;
y: number;
data: Data
}
interface Data {
name: string;
children: Data[]
}
通行证数据如下所示:
const data = {
name: "1",
children: [
{
name: "1.1",
children: [
{
name: "1.1.1",
},
{
name: "1.1.2",
},
{
name: "1.1.3",
},
],
},
{
name: "1.2",
},
],
};
解决方案
const data = {
name: "1",
children: [
{
name: "1.1",
children: [
{
name: "1.1.1",
},
{
name: "1.1.2",
},
{
name: "1.1.3",
},
],
},
{
name: "1.2",
},
],
};
数据对象不包含 x 字段,正如我所见,它仅包含名称和子字段。因此,您必须设置字段 x 然后才使用它
推荐阅读
- javascript - 使用 jQuery 检查 DataTables 数据是否不为空或为空
- javascript - 将谷歌地图 div 动态添加到 DOM
- maven - Maven 执行器插件非常慢 - 省略,保留解释
- scala - 将字段转换为整数、双精度数等。在 Spark Shell RDD 中的 Scala 中
- java - 如何从 ArrayList 的前 N 个值中获取最大值?
- r - 使用“ggpubr”在 R 中进行 T 测试
- r - 来自 GAM 的卡方值的子集导致 R
- python - 数据库用户不存在
- elasticsearch - 使用 elasticserch 版本 7 部署 zeebe
- php - 使用 Laravel 和 Mysql 处理超过 1 亿条记录