WordPress的古腾堡块 [英] Wordpress Gutenberg block

查看:127
本文介绍了WordPress的古腾堡块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用自定义块时遇到问题,当我重新加载版本页面时,该错误向我发送了错误消息.

I have a problem with a custom block which send me an error when I reload the edition page.

我不明白问题是什么.关于错误,实际与预期相同.

I don't understand what the problem is. In regards of the error, actual and expected are the same.

此处是错误:

块验证:namespace/nottomiss({对象})的块验证失败.

Block validation: Block validation failed for 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中提取数据的方式,当前您的选择器未针对内容.您可以将选择器更改为以下内容:

The selectors are how the editor pulls the data from the saved html, and currently your selectors aren't targeting the content. You could change your selectors to something like this:

attributes: {
  label: {
    type: 'string',
    source: 'html',
    selector: '.label'
  },
  title: {
    type: 'string',
    source: 'html',
    selector: '.title'
  }
}

您可以将保存功能更改为此:

And you could change your save function to this:

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>
  )
}

这篇关于WordPress的古腾堡块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆