MxCodec 解码功能不适用于 XML

MxCodec decode function not working with XML

我正在 javascript 使用 Angular7 和 MxGraph 库开发应用程序。我可以使用以下代码将 MxGraph 模型保存到 XML 中:

var enc = new mx.mxCodec(mx.mxUtils.createXmlDocument());
var node = enc.encode(editor.graph.getModel());
this.xml = mx.mxUtils.getPrettyXml(node);

但我无法根据 API 指示对其进行解码。我也试过粘贴 XML 并尝试像这样解码它但没有成功:

// Usually I would do this:
// var doc = mx.mxUtils.parseXml(this.xml);

// Just for testing purposes I did this
var doc = mx.mxUtils.parseXml(
           '<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/>'+
           '<Node0 label="Input" style="container" id="input"><mxCell style="container" vertex="1" connectable="0" parent="1">'+
           '<mxGeometry y="20" width="100" height="580" as="geometry"/></mxCell></Node0>'+
           '</root></mxGraphModel>');

var dec = new mx.mxCodec(doc);
dec.decode(doc.documentElement, graph.getModel());
console.log(graph.getModel());

在返回的模型中没有 Node0 的踪迹。

很抱歉回答了我自己的回复,但我终于在这里找到了线索: Editor doesn't created when mxGraph is used as npm module

将这些行添加到我的代码中解决了问题:

// Workaround because window['mxGraphModel'] is not defined
window['mxEditor'] = mx.mxEditor;
window['mxGeometry'] = mx.mxGeometry;
window['mxDefaultKeyHandler'] = mx.mxDefaultKeyHandler;
window['mxDefaultPopupMenu'] = mx.mxDefaultPopupMenu;
window['mxGraph'] = mx.mxGraph;
window['mxCell'] = mx.mxCell;
window['mxCellPath'] = mx.mxCellPath;
window['mxGraph'] = mx.mxGraph;
window['mxStylesheet'] = mx.mxStylesheet;
window['mxDefaultToolbar'] = mx.mxDefaultToolbar;
window['mxGraphModel'] = mx.mxGraphModel;