序列化

序列化会保存工作区的状态,以便进行加载 返回工作区。您需要将需要保存的所有数据 一种方便存储的文本格式

我们建议您将工作区序列化为 JSON。

如需了解详情,请参阅序列化

保存

以下代码段展示了如何将工作区的状态转换为 用于保存的 JSON:

// Serialize the state.
const state = Blockly.serialization.workspaces.save(myWorkspace);

// Then you save the state, e.g. to local storage.
localStorage.setItem('workspace-state', state);

加载

以下代码段展示了如何将某个已保存的状态加载到工作区中:

// Get your saved state from somewhere, e.g. local storage.
const state = localStorage.getItem('workspace-state');

// Deserialize the state.
Blockly.serialization.workspaces.load(state, myWorkspace);

这样系统就会创建所有已保存的块、变量和 工作区。