Closed moreira-dev closed 5 years ago
Thank you! 👌
Successfully published:
So i have to add in init.php wp_register_script( 'idb_image_slider-cgb-block-js', // Handle. plugins_url( '/dist/blocks.build.js', dirname( FILE ) ), // Block.build.js: We register the block here. Built with Webpack. array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' , 'wp-api-fetch'), // Dependencies, defined above. null, // filemtime( plugin_dir_path( DIR ) . 'dist/blocks.build.js' ), // Version: filemtime — Gets file modification time. true // Enqueue the script in the footer. );
and in blocks.build.js
import apiFetch from '@wordpress/api-fetch';
Allows using the external wp.apiFetch method. Otherwise, if you try to import it from the @wordpress/api-fetch package, it won't have the REST API base URL initialized.
Example of usage:
import apiFetch from '@wordpress/api-fetch';
apiFetch( { path: '/wp/v2/posts' } ).then( posts => { console.log( posts ); } );