作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为 WordPress 中的古腾堡编辑器实现一个 block 组件。我想使用 InnerBlocks
component例如,它也用于 Wordpress 本身提供的列组件。
当我尝试开始使用该组件时,我总是在前端遇到相同的错误:
在控制台中我收到消息:
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/
我是一名优秀的程序员,十分优秀!