翻閱古今
2023-11-02 21:54:41
這是我第一次創建自定義古騰堡塊。我已經能夠成功生成幾個塊。但是,當我創建帖子并使用該塊時,它可以工作,但是當我退出并返回帖子時,我收到此錯誤:該塊包含意外或無效內容我做了一些研究,發現如果編輯和保存的內容不一致,可能會導致這種情況。我一直無法找到不一致的地方,但我可以使用一些幫助。下面是注冊塊的代碼,該塊確實按預期執行,但在第一次創建塊后不允許我進行編輯。感謝幫助!const { registerBlockType } = wp.blocks;const { RichText, InspectorControls, ColorPalette, MediaUpload, RawHTML} = wp.editor;const { PanelBody, IconButton } = wp.components;registerBlockType('myblog/call-to-action', { title: 'Call To Action', description: 'Block to generate a call to action', icon: 'align-full-width', category: 'widgets', //Custom Attributes attributes: { title: { type: 'string', source: 'html', selector: 'h2' }, titleColor: { type: 'string', default: 'white' }, backgroundColor: { type: 'string', default: 'blue' }, body: { type: 'string', source: 'html', selector: 'p' } }, //Built-in Functions edit({attributes, setAttributes}) { const{ title, priceCard, titleColor, backgroundColor, } = attributes; //Custom Functions function onChangeTitle(newTitle) { setAttributes( { title: newTitle } ); } function onChangePriceCard(newCard) { setAttributes( { priceCard: newCard } ); } function onTitleColorChange(newColor){ setAttributes( { titleColor: newColor } ); } function onChangeBackgroundColor(newBackground) { setAttributes( { backgroundColor: newBackground }) }
1 回答

繁花如伊
TA貢獻2012條經驗 獲得超12個贊
priceCard您的屬性中缺少該屬性,例如:
registerBlockType('myblog/call-to-action', {
...
//Custom Attributes
attributes: {
...
priceCard: { // is missing
... // set type, default etc..
}
},
...
}
在更新的塊中添加/刪除了該屬性,然后重新加載保存的帖子時,該屬性丟失,從而導致問題。
此外,在進行簡單的屬性更新時,可以刪除代碼中的自定義函數部分,以便setAttributes()直接使用,例如:
<RichText
key="editable"
tagName="p"
placeholder="Paste Shortcode"
value={priceCard}
onChange={(value) => setAttributes({ priceCard: value })} // attribute: value
/>
這將使您的代碼更易于管理/故障排除。
添加回答
舉報
0/150
提交
取消