admin管理员组文章数量:1200396
I'm trying to get into Gutenberg Block Theme development and immediatly ran into an issue:
I am enquing a JavaScript file, because I want to add custom attributes to the existing blocks:
add_action( 'enqueue_block_editor_assets', function() {
wp_enqueue_script('awp-gutenberg-filters', get_template_directory_uri() . '/inc/js/gutenberg-filters.js', ['wp-edit-post']);
} );
Then, in my JavaScript file, I have this stuff:
const { createHigherOrderComponent } = wppose;
const { Fragment } = wp.element;
const { InspectorControls } = wp.editor;
const { PanelBody, SelectControl } = wpponents;
/**
* Create HOC to add spacing control to inspector controls of block.
*/
const withSpacingControl = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
// Do nothing if it's another block than our defined ones.
if ( ! enableSpacingControlOnBlocks.includes( props.name ) ) {
return (
<BlockEdit { ...props } />
);
}
const { spacing } = props.attributes;
// add has-spacing-xy class to block
if ( spacing ) {
props.attributes.className = `has-spacing-${ spacing }`;
}
return (
<Fragment>
<BlockEdit { ...props } />
<InspectorControls>
<PanelBody
title={ __( 'My Spacing Control' ) }
initialOpen={ true }
>
<SelectControl
label={ __( 'Spacing' ) }
value={ spacing }
options={ spacingControlOptions }
onChange={ ( selectedSpacingOption ) => {
props.setAttributes( {
spacing: selectedSpacingOption,
} );
} }
/>
</PanelBody>
</InspectorControls>
</Fragment>
);
};
}, 'withSpacingControl' );
addFilter( 'editor.BlockEdit', 'extend-block-example/with-spacing-control', withSpacingControl );
But I immediatly get this error in the console log on the line after the return, that says:
Uncaught SyntaxError: expected expression, got '<'
What am I doing wrong?
I'm trying to get into Gutenberg Block Theme development and immediatly ran into an issue:
I am enquing a JavaScript file, because I want to add custom attributes to the existing blocks:
add_action( 'enqueue_block_editor_assets', function() {
wp_enqueue_script('awp-gutenberg-filters', get_template_directory_uri() . '/inc/js/gutenberg-filters.js', ['wp-edit-post']);
} );
Then, in my JavaScript file, I have this stuff:
const { createHigherOrderComponent } = wp.compose;
const { Fragment } = wp.element;
const { InspectorControls } = wp.editor;
const { PanelBody, SelectControl } = wp.components;
/**
* Create HOC to add spacing control to inspector controls of block.
*/
const withSpacingControl = createHigherOrderComponent( ( BlockEdit ) => {
return ( props ) => {
// Do nothing if it's another block than our defined ones.
if ( ! enableSpacingControlOnBlocks.includes( props.name ) ) {
return (
<BlockEdit { ...props } />
);
}
const { spacing } = props.attributes;
// add has-spacing-xy class to block
if ( spacing ) {
props.attributes.className = `has-spacing-${ spacing }`;
}
return (
<Fragment>
<BlockEdit { ...props } />
<InspectorControls>
<PanelBody
title={ __( 'My Spacing Control' ) }
initialOpen={ true }
>
<SelectControl
label={ __( 'Spacing' ) }
value={ spacing }
options={ spacingControlOptions }
onChange={ ( selectedSpacingOption ) => {
props.setAttributes( {
spacing: selectedSpacingOption,
} );
} }
/>
</PanelBody>
</InspectorControls>
</Fragment>
);
};
}, 'withSpacingControl' );
addFilter( 'editor.BlockEdit', 'extend-block-example/with-spacing-control', withSpacingControl );
But I immediatly get this error in the console log on the line after the return, that says:
Uncaught SyntaxError: expected expression, got '<'
What am I doing wrong?
Share Improve this question asked Jun 12, 2022 at 9:26 Micke HasselqvistMicke Hasselqvist 153 bronze badges1 Answer
Reset to default 0The syntax where you return elements in tags like <Fragment>
is called JSX. Browsers do not support JSX, it’s just a syntax for easier development. You’ll need a build process to compile it into regular Javascript. Most documentation and tutorials cover this. For example, the official docs: https://developer.wordpress.org/block-editor/how-to-guides/platform/custom-block-editor/#code-syntax
本文标签: block editorGutenberg JavaScript error
版权声明:本文标题:block editor - Gutenberg JavaScript error 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738545912a2096413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论