“Cannot read property 'getEditedPostAttribute&

2019-07-27 12:03发布

I'm trying to implement a block component for the Gutenberg Editor in WordPress. There I want to use the InnerBlocks component which is also used for example for the columns component provided by Wordpress itself.

When I try to start using the component, I always get the same error in the frontend:

Error message from the frontend

In the console I get the message:

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)

I have implemented it similar according to this documentation 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>
        );
    }
});

My question is now, is there someone else who has this problem or how can I make this component work?

1条回答
Viruses.
2楼-- · 2019-07-27 12:44

So I have found the error myself. I have checked again the source script.build.js and found the following lines

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

I have only used the @wordpress/block-editor package. So I now added the following package to the script.js file and ran it again.

npm install @wordpress/editor

In the script.js

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

The error disappeared now but I got a new one unfortunately.

查看更多
登录 后发表回答