wordpress - Wordpress 古腾堡块
问题描述
我有一个自定义块的问题,当我重新加载版本页面时它会向我发送一个错误。
我不明白问题是什么。关于误差,实际和预期是相同的。
这里的错误:
块验证:namespace/nottomiss
({object})的块验证失败。
预期的:
<div class="wp-block-utopiales-nottomiss"><p>label test</p><p>label test</p></div>
实际的:
<div class="wp-block-utopiales-nottomiss"><p>label test</p><p>title test</p></div>
这是我的代码:
const { registerBlockType } = wp.blocks;
const { __ } = wp.i18n;
const { PanelBody, TextControl } = wp.components;
const { BlockControls, InspectorControls, RichText } = wp.editor;
const { createElement, Fragment } = wp.element
registerBlockType( 'namespace/nottomiss', {
title: __( 'Nottomiss' ),
description: __('My description'),
icon: 'star-filled',
category: 'widgets',
supports: { align: true, alignWide: true },
attributes: {
label: {
type: 'string',
source: 'html',
selector: 'p',
},
title: {
type: 'string',
source: 'html',
selector: 'p',
},
},
edit: function( props ) {
const { label, title } = props.attributes;
function onChangeLabel( newLabel ) {
props.setAttributes( { label: newLabel } );
}
function onChangeTitle( newTitle ) {
props.setAttributes( { title: newTitle } );
}
return (
<Fragment>
<BlockControls>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'List' ) }>
</PanelBody>
</InspectorControls>
<RichText
identifier="label"
tagName="p"
placeholder=""
value={ label }
onChange={ onChangeLabel }
/>
<RichText
identifier="title"
tagName="p"
placeholder=""
value={ title }
onChange={ onChangeTitle }
/>
</Fragment>
);
},
save: function( props ) {
const { label, title } = props.attributes;
return (
<div>
<RichText.Content
tagName="p"
value={ label }
/>
<RichText.Content
tagName="p"
value={ title }
/>
</div>
);
},
} );
提前感谢您的回答,
解决方案
选择器是编辑器从保存的 html 中提取数据的方式,目前您的选择器并未针对内容。您可以将选择器更改为以下内容:
attributes: {
label: {
type: 'string',
source: 'html',
selector: '.label'
},
title: {
type: 'string',
source: 'html',
selector: '.title'
}
}
您可以将保存功能更改为:
save: function(props) {
const { label, title } = props.attributes
return (
<div>
<div className="label">
<RichText.Content
value={ label }
/>
</div>
<div className="title">
<RichText.Content
value={ title }
/>
</div>
</div>
)
}
推荐阅读
- angular - 在 Mat Table 中创建一个独立于表头的表行?
- java - 将 Jackson 更新到 2.10 会破坏 Jetty 9。如何调试?
- prometheus - 普罗米修斯全球与本地限制?
- python - 如何根据行的“属性”将数据帧行乘以数组,同时保留与数组属性不匹配的数据帧行?
- git - 如何区分自己的git remote和github?
- docker - OCI 运行时执行失败
- android - 如何在存储库级别发出多个流值作为对象?
- ssl - 远程服务器的“PKIX 路径构建失败:”“无法找到到所请求目标的有效证书路径”
- python - 来自 AMS 的 TMF8701 ToF 距离传感器与 Python
- python - 程序只读取一次文件