我一直在尝试从 localStorage 中恢复所有使用过的块。我正在遍历所有本地保存的块,但出现以下错误。如果我能得到一些帮助,那就太好了。
错误:
blockly_compressed.js:1274 Uncaught Error: Blockly.Xml.textToDom expected an
<xml> document.
at Object.Blockly.Xml.textToDom (blockly_compressed.js:1274)
at restoreWorkspace ((index):312)
at HTMLButtonElement.onclick ((index):115)
Blockly.Xml.textToDom @ blockly_compressed.js:1274
restoreWorkspace @ (index):312
onclick @ (index):115
恢复所有块的代码:
// restore blocks
function restoreWorkspace() {
for (var i = 0; i < localStorage.length; i++){
let program=(localStorage.getItem(localStorage.key(i)));
Blockly.mainWorkspace.clear();
let textToDom = Blockly.Xml.textToDom(program);
Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, textToDom);
}
}
保存组合的代码:
function saveWorkspace() {
let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
let domToPretty = Blockly.Xml.domToPrettyText(xml);
var ranInt = Math.floor(Math.random() * 10);
window.localStorage.setItem("myProgram"+ranInt, domToPretty);
}