首页 > 解决方案 > mxCodec 无法正确解码 xml

问题描述

我在我的反应应用程序中集成了 mxGraph(mxgraphnpm 包),所以当我尝试使用 xml 加载图形时,我在控制台中收到错误

TypeError: geo.clone is not a function

我在单个 html 文件中所做的相同,它正在工作。

我调查并发现mxCellin react 应用程序与 html 应用程序不同。

在 HTML 的情况下,有填充geometry道具而不是反应(检查下面的屏幕)

Сan有人帮我正确解码xml吗?

mxCell从单个 HTML 控制台解码: https ://monosnap.com/file/yAHAi29zFGFpauqU2RtDcvmfPpZ0YJ

mxCell从 React 应用程序控制台解码: https ://monosnap.com/file/0XxPwyEracX7hMCnMHckAmI8Rl6OEh

React 组件的源代码:

const graph = new mx.mxGraph(this.automationRef.current)
new mx.mxRubberband(graph);

const xml = '<root>...</root>';
const doc = mx.mxUtils.parseXml(xml);
const codec = new mxCodec(doc);
let elt = doc.documentElement.firstChild;
const cells = [];
while (elt != null){
  const cell = codec.decodeCell(elt)
  cells.push(cell);
  graph.refresh();
  elt = elt.nextSibling;
}

graph.addCells(cells);

标签: reactjsmxgraph

解决方案


发现问题。这是解决方案:

https://github.com/jgraph/mxgraph/issues/301#issuecomment-514284868

引用:

你应该添加

window['mxGraphModel'] = mxGraphModel;
window['mxGeometry'] = mxGeometry;

let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());'

我发现解码方法解析xml需要windows param

结束报价


推荐阅读