gpt4 book ai didi

javascript - "Cannot read property ' getEditedPostAttribute ' of undefined"用于古腾堡中的 InnerBlocks

转载 作者:行者123 更新时间:2023-12-03 14:30:58 25 4
gpt4 key购买 nike

我正在尝试为 WordPress 中的古腾堡编辑器实现一个 block 组件。我想使用 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>
);
}
});

我现在的问题是,还有其他人遇到这个问题吗?或者我怎样才能让这个组件工作?

最佳答案

所以我自己发现了错误。我再次检查了源 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";

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

关于javascript - "Cannot read property ' getEditedPostAttribute ' of undefined"用于古腾堡中的 InnerBlocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56132541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com