亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的自定義古騰堡塊在保存時會損壞?

為什么我的自定義古騰堡塊在保存時會損壞?

翻閱古今 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

/>

這將使您的代碼更易于管理/故障排除。


查看完整回答
反對 回復 2023-11-02
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號