Wordpress Gutenberg block

孤街醉人 提交于 2019-12-24 10:41:07

问题


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.

Here the error :

Block validation: Block validation failed for namespace/nottomiss ({object}).

Expected:

<div class="wp-block-utopiales-nottomiss"><p>label test</p><p>label test</p></div>

Actual:

<div class="wp-block-utopiales-nottomiss"><p>label test</p><p>title test</p></div>

Here my code :

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

Thanks in advance for your answer,


回答1:


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


来源:https://stackoverflow.com/questions/54752295/wordpress-gutenberg-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!