如果我已经声明,viewScript
在我的街区。json文件。是否需要在register_block_type();
功能也?(我认为5.9没有必要?)
My Block.json
"textdomain": "postcode-pricing-wizard",
"editorScript": "file:./index.js",
"viewScript": "file:./view.js",
"style": "file:./style-index.css"
My Problem
我已经将脚本排入队列,如上所示,我可以看到一个完整的构建目录,也可以在编辑器中看到我的块。
view.js
然而,我的前端不是在加载吗?我不太清楚为什么?
除非我误解了医生的意思Block Editor Handbook - Metadata
{ "viewScript": "file:./build/view.js" }
// Block type frontend script definition.
// It will be enqueued only when viewing the content on the front of the site.
// Since WordPress 5.9.0 (My WP Version - 5.9)
--
这是我的register_block_type()
以备不时之需。
register_block_type( PPW_DIR_PATH . \'/build/pricing-wizard-block/\',
array(
\'render_callback\' => function( $attributes, $content ) {
if(!is_admin()) {
wp_localize_script( \'wp-block-ppw-postcode-pricing-wizard-js\', \'data\', [\'ajax_url\' => admin_url(\'admin-ajax.php\')]);
}
ob_start();
include_once plugin_dir_path( __FILE__ ) . \'/includes/block-editor/pricing-wizard-block/views\' . \'/block.php\';
return ob_get_clean();
},
)
);
最合适的回答,由SO网友:Beaniie 整理而成
Note to self, rest-up and re-read the docs.
所以答案是
is written in the docs, 但相当隐晦。如果您查看
Frontend Enqueueing 在块编辑器手册的第节中,您将看到以下语句。
Frontend Enqueueing #Frontend Enqueueing
Starting in the WordPress 5.8 release, it is possible to instruct WordPress to enqueue scripts and styles for a block type only when rendered on the frontend. It applies to the following asset fields in the block.json file:
- script
- viewScript (when the block defines render_callback during registration in PHP, then the block author is responsible for enqueuing the script)
- style
事实证明,我定义了
render_callback
所以我确实需要手动将我的
view.js
剧本
你知道,当你太累的时候,你会看到隧道状的视觉。是的,这就是其中一次。无论如何,谢谢你的阅读。我想我会为其他有类似问题的人负责。