我正在尝试使用UnitControl 在自定义Gutenberg块中定义文本的行高。我收到;此块遇到错误,无法预览;当我尝试访问WordPress编辑器中的块时。我不确定是什么导致了这个错误。我甚至尝试在返回中删除对UnitControl的引用,并保存如下lineHeight:headlineSpacing
查看是否会停止标记错误。它仍然会给我错误,所以我不确定我是否正确使用了UnitControl,或者我是否在<UnitControl>
要素
CODE:
const { registerBlockType } = wp.blocks;
const {
RichText,
InspectorControls,
ColorPalette,
MediaUpload,
MediaUploadCheck,
Button,
RawHTML,
FontSizePicker,
__experimentalUnitControl,
InnerBlocks
} = wp.blockEditor;
const { PanelBody, IconButton } = wp.components;
registerBlockType(\'mycustomsite/small-jumbo\', {
//Built-in Attributes
title: \'Small Jumbo\',
description: \'Block to generate a smaller jumbotron\',
icon: \'align-full-width\',
category: \'design\',
//Custom Attributes
attributes: {
title: {
type: \'string\',
source: \'html\',
selector: \'h1\'
}
},
//Built-in Functions
edit({attributes, setAttributes}) {
const{
title,
headlineSize,
headlineSpacing,
} = attributes;
const headlineSizes = [
{
name: \'14px\',
slug: \'fourteen\',
size: 14
},
{
name: \'16px\',
slug: \'sixteen\',
size: 16
},
{
name: \'18px\',
slug: \'eighteen\',
size: 18
},
{
name: \'24px\',
slug: \'twentyfour\',
size: 24
}
];
//Custom Functions
function onChangeTitle(newTitle) {
setAttributes( { title: newTitle } );
}
function onChangeHeadlineSize( newHeadlineSize ) {
setAttributes( { headlineSize: newHeadlineSize } );
}
function onChangeHeadlineSpacing( newHeadlineSpacing) {
setAttributes( { headlineSize: newHeadlineSpacing } );
}
return ([
<InspectorControls style={ { marginBottom: \'40px\' } }>
<p><strong>Customize Top Text</strong></p>
<PanelBody title={ \'Headline Size\' }>
<FontSizePicker
fontSizes={ headlineSizes }
value={ headlineSize }
onChange={ onChangeHeadlineSize }
/>
</PanelBody>
<PanelBody title={ \'Line Height\' }>
<UnitControl
label="Line Height"
isUnitSelectTabbable
value={ headlineSpacing }
onChange={ onChangeHeadlineSpacing }
/>
</PanelBody>
</InspectorControls>,
<div>
<RichText
key="editable"
tagName="h1"
placeholder="H1 Title"
value= { title }
onChange= { onChangeTitle }
style= { { fontSize:headlineSize, lineHeight:headlineSpacing} }
/>
</div>
]);
},
save({ attributes }) {
const {
title,
headlineSize,
headlineSpacing,
} = attributes;
return (
<div>
<RichText.Content style={ {fontSize:headlineSize, lineHeight:headlineSpacing } } tagName="h1" value={title} />
</div>
);
}
});
最合适的回答,由SO网友:Sally CJ 整理而成
代码中的问题是UnitControl
实际上没有在任何地方定义:
如果您查看documentation, 这个UnitControl
定义如下:
import { __experimentalUnitControl as UnitControl } from \'@wordpress/components\';
一、 e.它使用
__experimentalUnitControl
中的组件
wp.components
而不是
wp.blockEditor
, 其次,它将组件导入为
UnitControl
.
所以你也应该这样做。
以及与const
关键字为:
const { __experimentalUnitControl: UnitControl } = wp.components;
// I.e. const { <actual name>: <alias> }
因此,您应该删除
__experimentalUnitControl,
行中的代码并导入
wp.components
(或
@wordpress/components
包装)代替。一、 e。
// Change this:
const { PanelBody, IconButton } = wp.components;
// to this one:
const {
PanelBody,
IconButton,
__experimentalUnitControl: UnitControl
} = wp.components;
因此,错误;此块遇到错误;现在就不见了。
代码中的其他问题:
您的onChangeTitle()
代码:您拼错了setAttributes
像etAttributes
它会导致错误,因为etAttributes
未定义。:)删除>其次,如果返回元素数组,则应设置key 对于每个元素:
return ([ // add a key for each direct child
<InspectorControls style={ { marginBottom: \'40px\' } } key="foo-key">
...
</InspectorControls>,
<div key="bar-key">
...
</div>
]);