古腾堡中InnerBlocks的“ TypeError:无法读取null的属性'innerBlocks'”

时间:2019-05-16 11:17:39

标签: javascript wordpress reactjs wordpress-gutenberg

我最近问了一个关于同一topic的问题。在那里,我自己找到了问题。但不幸的是,我遇到了一个新错误:

TypeError: Cannot read property 'innerBlocks' of null
    at InnerBlocks.synchronizeBlocksWithTemplate (script.build.js?ver=1:149405)
    at InnerBlocks.componentDidMount (script.build.js?ver=1:149366)
    at zc (react-dom.min.js?ver=16.6.3:146)
    at wc (react-dom.min.js?ver=16.6.3:138)
    at fa (react-dom.min.js?ver=16.6.3:137)
    at ng (react-dom.min.js?ver=16.6.3:149)
    at Se (react-dom.min.js?ver=16.6.3:40)

所以我用相同的代码尝试过,但是我没有用:

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

import {TextControl} from '@wordpress/components';

import {InnerBlocks} from '@wordpress/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>
        );
    }
});

然后,我检查了作为插件嵌入的内置script.js文件。在这种情况下,this.props.blocknull

key: "componentDidMount",
value: function componentDidMount() {
  var innerBlocks = this.props.block.innerBlocks; // only synchronize innerBlocks with template if innerBlocks are empty or a locking all exists

  if (innerBlocks.length === 0 || this.getTemplateLock() === 'all') {
    this.synchronizeBlocksWithTemplate();
  }

  if (this.state.templateInProcess) {
    this.setState({
      templateInProcess: false
    });
  }
}

更新

变量this.props具有以下变量:

Variables of this.props

有人遇到相同的问题或有解决方法吗?

1 个答案:

答案 0 :(得分:0)

我再次解决了自己的问题。最后,对我来说,这个问题尚不清楚,但确实可行。

我现在要做的只是将wp.editor用作InnerBlocks组件的导入实例。这样,它现在可以加载完整的InnerBlocks编辑器,现在我可以在其中添加自定义块了。

const {registerBlockType} = wp.blocks;
const {InspectorControls, RichText, InnerBlocks} = wp.editor; //Imported the InnerBlocks from this source.

import {CheckboxControl, TextControl} from '@wordpress/components';

// Removed this line
// import {InnerBlocks} from "@wordpress/editor";