将前端js文件添加到自定义块的问题

问题描述 投票:0回答:0

我正在写一个自定义块,我想注册一个前端javascript文件。我在根目录中添加了一个 webpack.config.js 文件:

const defaultConfig = require('@wordpress/scripts/config/webpack.config');
const path = require('path');


module.exports = {
    ...defaultConfig,
    entry: {
       ...defaultConfig.entry,
       gutrstimedmessagefrontscript: './src/gutrs-timed-message-front.js'
   }
}

然后在我的 src 文件夹中,我创建了一个名为:gutrs-timed-message-front.js 的文件

然后在我的主要 php 插件文件中我尝试了这个:

function create_block_timed_message_block_init() {

//register scripts
wp_register_script(
    'gutrs-timed-message-front-script',
    plugins_url( 'build/gutrstimedmessagefrontscript.js', __FILE__ ),
    [],
    true,
            true
);

register_block_type( __DIR__ ,array(
    'script' => 'gutrs-timed-message-front-script'
) );
}
 add_action( 'init', 'create_block_timed_message_block_init' );

但是随后方块从方块插入器中消失了。我在这里错过了什么?或者我做错了什么?

wordpress block
© www.soinside.com 2019 - 2024. All rights reserved.