首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >古腾堡:保存中的动态块ToggleControl错误

古腾堡:保存中的动态块ToggleControl错误
EN

WordPress Development用户
提问于 2020-11-23 17:16:17
回答 1查看 549关注 0票数 1

我正在创建一个动态块,使用Toggle开关或前端的复选框在停用/激活时正确显示,但在后端,当saving:<#>“更新失败”时,我会得到此错误。响应不是有效的JSON响应。

这是我的js:

代码语言:javascript
复制
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;

const { InspectorControls} = wp.blockEditor;
const { PanelBody,ToggleControl } = wp.components;

const BlockEdit = (props) => {
    const { attributes, setAttributes } = props;
    const { haveRead } = attributes

    return(
        
        
            
            

                
                     setAttributes({ haveRead: checked })}
              />
                

            
        
        
      
    );
    

}

registerBlockType('df/portfolioblock', {
    title: __('DF Portfolio', 'df-portfolio'),
    icon: 'portfolio',
    category: 'df-block',
    attributes: {
        haveRead: {
            type: 'boolean',
            selector: 'js-book-details-read'
          },
       
    },
    save: () => { return null; },
    edit:BlockEdit, 

});

这是php:

代码语言:javascript
复制
add_action('init', function() {
    wp_register_script('df-portfolio-block-js',  plugins_url( 'assets/js/block-df-portfolio.js', __FILE__ ),
    ['wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-data']
);
   
    register_block_type('df/portfolioblock', [
        'editor_script' => 'df-portfolio-block-js',
        'editor_style' => 'df-portfolio-block-style',
        'render_callback' => 'df_gutenberg_portfolio_render',
        'attributes' => [
            'haveRead'=>['type'=> 'boolean','default'=> false],
                
        ]
    ]);

});


function df_gutenberg_portfolio_render($attributes, $content) {
    
    
    $book_details_have_read = $attributes['haveRead'];

?>



    
        This book has been read.

任何帮助都将不胜感激,谢谢。

EN

回答 1

WordPress Development用户

发布于 2020-11-23 23:06:46

我设法让它正常工作,下面是正确的代码:

JS文件

代码语言:javascript
复制
const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;

const { InspectorControls} = wp.blockEditor;
const { PanelBody,ToggleControl } = wp.components;

const BlockEdit = (props) => {
    const { attributes, setAttributes } = props;
    const { haveRead } = attributes

    return(

        

            

                
                         setAttributes({ haveRead: checked })}
                />
                

            


        
    );


}

registerBlockType('df/portfolioblock', {
    title: __('DF Portfolio', 'df-portfolio'),
    icon: 'portfolio',
    category: 'df-block',
    supports: {
        // Turn off ability to edit HTML of block content
        html: false,
        // Turn off reusable block feature
        reusable: false,
        // Add alignwide and alignfull options
        align: false
        },
    attributes: {
        haveRead: {
            type: 'boolean',
            selector: 'js-book-details-read'
            },

    },
    save: props => {
        return null
        },
    edit:BlockEdit,

});

PHP文件

代码语言:javascript
复制
add_action('init', function() {
    wp_register_script('df-portfolio-block-js',  plugins_url( 'assets/js/block-df-portfolio.js', __FILE__ ),
        ['wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-data','wp-i18n']
    );

    register_block_type('df/portfolioblock', [
        'editor_script' => 'df-portfolio-block-js',
        'editor_style' => 'df-portfolio-block-style',
        'render_callback' => 'df_gutenberg_portfolio_render',
    ]);

});


function df_gutenberg_portfolio_render($attributes, $content) {

    $book_details_have_read = $attributes['haveRead'];
    ob_start(); // Turn on output buffering

    ?>


        
            This book has been read.
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/378683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档