問(wèn)題原因:
antvx6中的setChildren()、setParent()都是單向綁定,如果使用setChildren()或設(shè)置父節(jié)點(diǎn)的children屬性,只能讓父認(rèn)子,而子不認(rèn)父,父節(jié)點(diǎn)調(diào)getChildren()有值,而子節(jié)點(diǎn)調(diào)getParent()為null。
解決方案:
使用addChild()可以雙向綁定。如果是通過(guò)預(yù)設(shè)數(shù)據(jù)的方式,可以在父節(jié)點(diǎn)設(shè)置children綁定子節(jié)點(diǎn),同時(shí)在子節(jié)點(diǎn)設(shè)置parent綁定父節(jié)點(diǎn),實(shí)現(xiàn)雙向綁定。
預(yù)設(shè)數(shù)據(jù):
const children = {
id: 'children',
shape: 'custom-node',
label: 'childrenNode',
...
parent: 'parent' // 綁定父節(jié)點(diǎn),getParent()有值,否則無(wú)值
}
const parent = {
id: 'parent',
shape: 'custom-node',
label: 'parentNode',
...
children: ['children'] // 綁定子節(jié)點(diǎn)
}
Graph.fromJSON([children, parent])
調(diào)用API:
const children = graph.addNode({
shape: 'custom-node',
label: 'childrenNode'
})
const parent = graph.addNode({
shape: 'custom-node',
label: 'parentNode'
})
parent.setChildren([children]) // 單向綁定,入?yún)閿?shù)組,子項(xiàng)getParent()為null
parent.addChild(children) // 雙向綁定,入?yún)閏ell對(duì)象