在我的自定义块插件中,我使用创建块来生成块环境以进行块编辑。在我的‘`src/ed.js文件中,我有以下设置.
import { __ } from '@wordpress/i18n';
import {
useBlockProps,
Fragment,
RichText,
BlockControls,
} from '@wordpress/block-editor';
import './editor.scss';
export default function Edit( { attributes, setAttributes } ) {
const { text } = attributes;
// additional options for our RichText component - https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/rich-text/README.md
return (
<Fragment>
<BlockControls controls={[{
title: "Button 1",
icon: "admin-generic",
onClick : () => console.log("Button 1 clicked!"),
},]} />
<RichText
{ ...useBlockProps() }
onChange={ ( value ) => setAttributes( { text: value } ) }
value={ text }
placeholder={ __( 'My chocolate placeholder', 'chocolate' ) }
tagName="h4"
allowedFormats={ 'core/bold' }
/>
</Fragment>
);
}
...But当我运行npm,并转到我的编辑页面时,我在控制台中得到以下错误.
Block validation: Block validation failed for `course-blocks/chocolate` ( ....}
我的save.js文件看起来如此..。
import { useBlockProps, RichText } from '@wordpress/block-editor';
export default function save( { attributes } ) {
const { text } = attributes;
return (
<RichText.Content
{ ...useBlockProps.save() }
tagName="h4"
value={ text }
/>
);
}
我的属性正被从我的block.json.
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "course-blocks/chocolate",
"version": "0.1.0",
"title": "Chocolate",
"category": "text",
"description": "A playground for block development.",
"supports": {
"html": false
},
"textdomain": "chocolate",
"editorScript": "file:./index.js",
"editorStyle": "file:./index.css",
"style": "file:./style-index.css",
"attributes": {
"text": {
"type": "string",
"source": "html",
"selector": "h4"
}
}
}
发布于 2023-03-14 16:47:01
Fragment
导出位于@wordpress/element
包中,而不是block-editor
中。
尽管如此,您不再需要显式地编写<Fragment>
(截至反应16.2)。相反,您只需使用“空标签”:
return (
<>
<p>One</p>
<p>Two</p>
</>
);
https://wordpress.stackexchange.com/questions/414633
复制相似问题