我是sigma.js的新手,我在Angular 7中使用sigma.js,将邻居方法添加到图中看起来不错。另外,第一次图表看起来不错,但是当我更改路线并再次调用图表页面时,出现了类似ERROR错误的错误:未捕获(承诺):方法“邻居”已经存在。
我的代码像
sigma.classes.graph.addMethod("neighbors", function(nodeId) {
var k,
neighbors = {},
index = this.allNeighborsIndex[nodeId] || {};
for (k in index) neighbors[k] = this.nodesIndex[k];
return neighbors;
});
this.s = new sigma({
graph: this.graph,
renderer: {
container: document.getElementById("sigma-container"),
type: "canvas"
},
settings: {
defaultEdgeType: "line",
defaultNodeColor: "#aaabaa",
autoRescale: ["nodePosition", "nodeSize"],
labelThreshold: 0,
adjustSizes: true,
enableEdgeHovering: true,
edgeHoverColor: "#393a3c",
defaultEdgeHoverColor: "#393a3c",
edgeHoverExtremities: true
}
});
this.s.graph.nodes().forEach(function(n) {
n.originalColor = n.color;
});
this.s.graph.edges().forEach(function(e) {
e.originalColor = e.color;
});
this.s.bind("overNode", e => {
this.createSelectTeamMemberGraph(e);
});
}
public createSelectTeamMemberGraph(user) {
let e = user;
var nodeId = user.userId || e.data.node.id,
toKeep = this.s.graph.neighbors(nodeId);
toKeep[nodeId] = e || e.data.node;
this.s.graph.nodes().forEach(function(n) {
if (toKeep[n.id]) n.color = n.originalColor;
else n.color = "#aaaaab";
});
this.s.graph.edges().forEach(function(e) {
if (toKeep[e.source] && toKeep[e.target]) e.color = e.originalColor;
else e.color = "#aaaaab";
});
this.s.refresh();
}
答案 0 :(得分:0)
由于sigma js已经加载一次并且添加了名为“ neighbor”的方法,因此会出现此错误。如果将此方法再次添加到sigma js配置中,则会显示此错误“方法已存在”。
解决方法是只调用代码的以下部分:
sigma.classes.graph.addMethod("neighbors", function(nodeId) {
var k,
neighbors = {},
index = this.allNeighborsIndex[nodeId] || {};
for (k in index) neighbors[k] = this.nodesIndex[k];
return neighbors;
});
这可以通过在单独的js文件中编写这部分代码来完成,该文件仅被调用一次。