gpt4 book ai didi

javascript - WordPress 阻止注册 ES6/ESNext Uncaught SyntaxError : Unexpected token <

转载 作者:行者123 更新时间:2023-11-29 23:15:40 26 4
gpt4 key购买 nike

我一直在到处寻找为什么我的代码不能工作。我正在尝试使用以下代码创建一个 WordPress Gutenberg Block。我已经测试了来自不同网站的 3 个版本的代码,但无法弄清楚为什么它在 <div className={className}> 上失败了。

PHP - functions.php

function register_block_editor_assets() {
$dependencies = array(
'wp-blocks', // Provides useful functions and components for extending the editor
'wp-i18n', // Provides localization functions
'wp-element', // Provides React.Component
'wp-components' // Provides many prebuilt components and controls
);
wp_register_script( 'my-block-editor', get_stylesheet_directory_uri().'/js/testing2.js', $dependencies );
}
add_action( 'admin_init', 'register_block_editor_assets' );

function register_block_assets() {
wp_register_script( 'my-block', get_stylesheet_directory_uri().'/js/testing2.js', array( 'jquery' ) );
}
add_action( 'init', 'register_block_assets' );

JS - testing2.js

const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;
const {
RichText,
BlockControls,
AlignmentToolbar,
} = wp.editor;
registerBlockType( 'example/example-block', {
title = __('Example Block', 'example'),
icon = 'screenoptions',
category = 'common',
attributes = {
content: { // Parsed from HTML selector
source: 'children',
selector: 'p',
type: 'array'
},
textColor: { // Serialized by default
type: 'string'
},
isPinned: { // Pulled from REST API
type: 'boolean',
source: 'meta',
meta: 'example_is_pinned'
}
},
edit = ({ attributes, setAttributes, className, isSelected }) => {
const {
content
} = attributes
return (
<div className={className}>
<RichText
className="example-content"
value={content}
onChange={(content) =>setAttributes({ content })} />
</div>
)
}),
save = ({ attributes }) {
const {
content
} = attributes
return (
<div>
<p>{content}</p>
</div>
)
})
};

最佳答案

您的 js 文件中存在语法错误。

试试这段代码

const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;
const {
RichText,
BlockControls,
AlignmentToolbar,
} = wp.editor;
registerBlockType( 'example/example-block', {
title :__('Example Block', 'example'),
icon : 'screenoptions',
category : 'common',
attributes : {
content: { // Parsed from HTML selector
source: 'children',
selector: 'p',
type: 'array'
},
textColor: { // Serialized by default
type: 'string'
},
isPinned: { // Pulled from REST API
type: 'boolean',
source: 'meta',
meta: 'example_is_pinned'
}
},
edit(props){
const {attributes, setAttributes, className} = props;
const {
content
} = attributes
return (
<div className={className}>
<RichText
className="example-content"
value={content}
onChange={(content) =>setAttributes({ content })} />
</div>
)
},
save(props){
const {attributes} = props;
const {
content
} = attributes
return (
<div>
<p>{content}</p>
</div>
)
} });

关于javascript - WordPress 阻止注册 ES6/ESNext Uncaught SyntaxError : Unexpected token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52896463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com