Blockly 已於 2025 年 11 月 10 日移交給 Raspberry Pi Foundation!請參閱網誌文章和常見問題。
Google uses AI technology to translate content into your preferred language. AI translations can contain errors.
使用內建程序方塊
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用外掛程式
如要使用程序區塊,建議使用 @blockly/block-shareable-procedures 外掛程式。如要瞭解外掛程式區塊和內建區塊的差異,請參閱總覽。
安裝
Yarn:
shell
yarn add @blockly/block-shareable-procedures
NPM:
shell
npm install @blockly/block-shareable-procedures
用量
import Blockly from 'blockly';
import {blocks, unregisterProcedureBlocks} '@blockly/block-shareable-procedures';
unregisterProcedureBlocks();
Blockly.common.defineBlocks(blocks);
這會定義與舊版內建程序區塊同名的程序區塊。因此,如果您載入以舊程序方塊儲存的 JSON 或 XML,這些檔案仍會繼續正常載入。
定義好區塊後 (外掛程式或舊版內建區塊),您需要讓使用者能夠使用這些區塊。這需要使用類別樣式工具箱,因為程序類別是動態填入,而飛出式工具箱不支援這項功能。
您可以將動態類別新增至工具箱,如下所示:
JSON
{
"kind": "categoryToolbox",
"contents": [
{
"kind": "category",
"name": "Functions",
"custom": "PROCEDURE"
}
]
};
XML
<xml id="toolbox" style="display: none">
<category name="Functions" custom="PROCEDURE">
</xml>
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2026-05-13 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2026-05-13 (世界標準時間)。"],[],["The `@blockly/block-shareable-procedures` plugin is recommended for using procedure blocks. Installation is done via Yarn or NPM. Usage involves importing `Blockly` and the plugin's `blocks`, then using the `unregisterProcedureBlocks()` and `Blockly.common.defineBlocks(blocks)` methods. This defines procedure blocks that maintain compatibility with legacy blocks. To make the blocks accessible, add a \"Functions\" category with the custom attribute \"PROCEDURE\" to your category-style toolbox, which is compatible with the dynamic procedure category.\n"]]