序列化
序列化會儲存工作區的狀態,以便您進行載入
稍後返回工作區您可以將所有需要儲存的資料轉換為文字格式,方便儲存。
建議您將工作區序列化為 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);
這樣就能建立所有已儲存的區塊、變數和
工作區
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-04 (世界標準時間)。
[null,null,["上次更新時間:2024-11-04 (世界標準時間)。"],[[["Serialization allows you to save the current state of your Blockly workspace (blocks, variables, etc.) into a text-based format, typically JSON, for later retrieval."],["You can save the serialized workspace data to a storage location like local storage and then load it back into a workspace to restore its previous state."],["Blockly provides APIs (`Blockly.serialization.workspaces.save` and `Blockly.serialization.workspaces.load`) for easily serializing and deserializing workspace states."]]],["Serialization saves workspace states for later loading by converting data into a text-based format, preferably JSON. To save, use `Blockly.serialization.workspaces.save(myWorkspace)` to get the state, then store it (e.g., in local storage). To load, retrieve the saved state and use `Blockly.serialization.workspaces.load(state, myWorkspace)` to recreate blocks, variables, and other elements in the workspace.\n"]]