【问题标题】:ServerSideRender attributes does not passed to backendServerSideRender 属性不传递给后端
【发布时间】:2021-11-13 17:18:39
【问题描述】:

我正在使用 @wordpress/server-side-render 从后端获取 Gutenberg 块的内容。

if ( props && props.attributes ) {
    blockContent = <ServerSideRender
        block="test/employee-block"
        attributes={ props.attributes }
    />
}

这里是块的 PHP 端

register_block_type( 'test/employee-block', array(
        'title' => 'Test Block',
        'description' => 'test',
        'category' => 'widgets',
        'icon' => 'admin-users',
        'api_version' => 2,
        'editor_script' => 'employees_dynamic_block_script',
        'render_callback' => function ($block_attributes, $content) {
            return '<h1>test</h1>';
        }
    ) );

但是,渲染回调函数的 $block_attributes 始终为空。我不知道为什么,但根据API documentation,它应该在那里。

【问题讨论】:

标签: php reactjs wordpress wordpress-gutenberg gutenberg-blocks


【解决方案1】:

找到了解决方法,如果你没有在 register_block_type 函数中定义参数键和类型,它不会将它们传递给渲染回调。

register_block_type( 'test/employee-block', array(
        'api_version' => 2,
        'editor_script' => 'employees_dynamic_block_script',
        'attributes'      => array(
            'selectControl'             => array(
                'type' => 'string',
                'default' => '0',
            )
        ),
        'render_callback' => function ($block_attributes, $content) {
            return '<h1>test</h1>';
        }
    ) );

【讨论】:

  • 这让我有好几次...一定要在组件本身中也传递它们。
猜你喜欢
  • 2018-11-16
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 2022-11-16
  • 2021-07-01
相关资源
最近更新 更多