古腾堡中InnerBlocks的“无法读取未定义的属性'getEditedPostAttribute'”

时间:2019-05-14 14:15:42

标签: javascript wordpress reactjs wordpress-gutenberg

我正在尝试为WordPress中的Gutenberg编辑器实现一个块组件。在这里我想使用InnerBlocks component,它也用于Wordpress本身提供的列组件。

当我尝试开始使用该组件时,总是在前端遇到相同的错误:

Error message from the frontend

在控制台中,我收到消息:

TypeError: Cannot read property 'getEditedPostAttribute' of undefined
    at script.build.js?ver=1:27811
    at getNextMergeProps (script.build.js?ver=1:103469)
    at new ComponentWithSelect (script.build.js?ver=1:103487)
    at zf (react-dom.min.js?ver=16.6.3:69)
    at Mf (react-dom.min.js?ver=16.6.3:87)
    at ph (react-dom.min.js?ver=16.6.3:98)
    at eg (react-dom.min.js?ver=16.6.3:125)
    at fg (react-dom.min.js?ver=16.6.3:126)
    at wc (react-dom.min.js?ver=16.6.3:138)
    at fa (react-dom.min.js?ver=16.6.3:137)

根据本文档here,我已经实现了类似的方法:

const {registerBlockType} = wp.blocks;
const {InspectorControls, RichText, MediaUpload} = wp.editor;

import {TextControl} from '@wordpress/components';
import {InnerBlocks} from '@wordpress/block-editor';

let blockStyle = {
    marginTop: "25px",
    marginBottom: "25px;"
};

registerBlockType('myself/test-component', {
    title: 'Test component',
    icon: 'editor-insertmore',
    category: 'common',
    attributes: {
        title: {
            type: 'string'
        }
    },

    edit(props) {
        const {setAttributes, attributes} = props;

        function setTitle(changes) {
            setAttributes({
                title: changes
            })
        }

        return (
            <div style={blockStyle}>
                <TextControl
                    placeholder="Titel"
                    value={attributes.title}
                    onChange={setTitle}
                />
                <InnerBlocks
                    templateLock={false}
                    renderAppender={(
                        () => <InnerBlocks.ButtonBlockAppender/>
                    )}
                />
            </div>
        )
    },

    save(props) {
        const {attributes, className} = props;

        return (
            <div style={blockStyle}>
                <InnerBlocks.Content/>
            </div>
        );
    }
});

我的问题是,现在还有其他人遇到此问题吗?或者该如何使该组件正常工作?

2 个答案:

答案 0 :(得分:0)

所以我自己发现了错误。 我再次检查了源script.build.js,发现以下几行

var _select2 = select('core/editor'),
      getEditedPostAttribute = _select2.getEditedPostAttribute;

我只使用了@ wordpress / block-editor软件包。因此,我现在将以下包添加到script.js文件中,然后再次运行它。

npm install @wordpress/editor

在script.js中

import {InnerBlocks} from "@wordpress/editor";

错误现在消失了,但不幸的是我得到了一个新错误。

答案 1 :(得分:0)

我刚刚遇到此错误,我不确定我使用的WordPress实例是否使用Gutenberg,但是我可以通过禁用Yoast SEO插件来消除错误,如建议here