【问题标题】:WordPress InnerBlocks Gutenberg don't save correctWordPress InnerBlocks Gutenberg 不正确保存
【发布时间】:2021-03-31 02:27:00
【问题描述】:

我有一个非常简单的 InnerBlock。当我打开一个页面时,我可以毫无问题地创建一个嵌套块。我可以保存并查看页面一切都是正确的。但是,当我再次尝试编辑页面时,我收到 Block validation failed 错误:

Content generated by `save` function:

<div class="container my-4 my-md-5" class="wp-block-wpptheme-faq-section"><div class="row"><div class="col-12"><div class="accordion" id="faq"><div></div></div></div></div></div>

Content retrieved from post body:

<div class="container my-4 my-md-5" class="wp-block-wpptheme-faq-section"><div class="row"><div class="col-12"><div class="accordion" id="faq"><div>

为什么区块没有正确保存?

const ALLOWED_BLOCKS = ['wpptheme/faq-block'];

registerBlockType('wpptheme/faq-section', {
    //built-in attributes
    title: 'FAQ Abschnitt',
    icon: 'star-filled',
    category: 'wpptheme',

    edit() {
        return ([
            <div class="wpptheme-custom">
                <h3>Häufig gestelle Fragen</h3>
                <div>
                    <InnerBlocks allowedBlocks={ ALLOWED_BLOCKS } />
                </div>
            </div>
        ]);
    },
    save() {

        return (
            <div class="container my-4 my-md-5">
                <div class="row">
                    <div class="col-12">
                        <div class="accordion" id="faq">
                            <div>
                                <InnerBlocks.Content />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
});


registerBlockType('wpptheme/faq-block', {
    //built-in attributes
    title: 'FAQ Block',
    icon: 'star-filled',
    parent: [ 'wpptheme/faq-section' ],
    category: 'wpptheme',

    edit() {
        return ([
            <div class="wpptheme-custom">
                <h4>Block</h4>
            </div>
        ]);
    },
    save() {
        return (
            <h3>Test</h3>
        );
    }
});

【问题讨论】:

    标签: javascript php reactjs wordpress wordpress-gutenberg


    【解决方案1】:

    save()wpptheme/faq-section 中如何应用这些类的问题。 React should be passed in with "className" property 中的 CSS 类,与普通 HTML 不同。

    为 wpptheme/faq-section 更新了 save()

    ...
        save() {
    
            return (
                <div className="container my-4 my-md-5">
                    <div className="row">
                        <div className="col-12">
                            <div className="accordion" id="faq">
                                <div>
                                    <InnerBlocks.Content />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
        );
    }
    ...
    

    【讨论】:

    • 有效!完美:)我以为你可以混合这些。为什么我可以在编辑中使用 class="wpptheme-custom" ?而不是类名。是 React 的展位?
    • 在使用 React 时最好同时/一直使用 className(即使它不会在 Gutenberg 的 edit() 中触发错误)。块验证会触发 save() 中的错误,这可能也是 edit() 应该发生的情况..
    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多