admin管理员组文章数量:1130349
I'm working with the latest Gutenberg and WP and until last week, the code below worked as expected.
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
All of a sudden I'm getting wp.apiFetch is not a function errors.
Anyone have any idea why??
I'm working with the latest Gutenberg and WP and until last week, the code below worked as expected.
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
All of a sudden I'm getting wp.apiFetch is not a function errors.
Anyone have any idea why??
Share Improve this question asked Nov 28, 2018 at 19:12 Chad HoldenChad Holden 671 silver badge7 bronze badges 5 |1 Answer
Reset to default 3as @Alvero pointed out in the comments, instead of just supplying wp-api in the block registration, you now need to specify wp-api-fetch.
$index_js = 'sample-post/index.js';
wp_register_script(
'sample-post-block-block-editor',
plugins_url( $index_js, __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-api-fetch',
),
filemtime( "$dir/$index_js" )
);
Then within your block, you call it using the wp.apiFetch function:
var registerBlockType = wp.blocks.registerBlockType,
el = wp.element.createElement,
__ = wp.i18n.__,
apiFetch = wp.apiFetch;
const postSelections = [];
const allPosts = apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
I'm working with the latest Gutenberg and WP and until last week, the code below worked as expected.
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
All of a sudden I'm getting wp.apiFetch is not a function errors.
Anyone have any idea why??
I'm working with the latest Gutenberg and WP and until last week, the code below worked as expected.
const postSelections = [];
const allPosts = wp.apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
All of a sudden I'm getting wp.apiFetch is not a function errors.
Anyone have any idea why??
Share Improve this question asked Nov 28, 2018 at 19:12 Chad HoldenChad Holden 671 silver badge7 bronze badges 5-
It looks like you are not adding the
wp-api-fetchdependency in your script. – Alvaro Commented Nov 28, 2018 at 19:37 - I'm loading that through the PHP end: wp_register_script( 'featured-post-block-block-editor', plugins_url( $index_js, FILE ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-api', ), filemtime( "$dir/$index_js" ) ); – Chad Holden Commented Nov 28, 2018 at 20:18
-
Add also
'wp-api-fetch'and it should not throw the error. – Alvaro Commented Nov 28, 2018 at 20:24 - Winner winner, chicken dinner! Submit it as an answer if you want credit. The change from wp-api to wp-api-fetch worked. Odd though because up until version Gutenberg 4.4 wp-api alone worked. – Chad Holden Commented Nov 28, 2018 at 20:32
- Things have been changing much last months in Gutenberg, it was a bit difficult sometimes to follow up, but it is already in RC. My suggestion is to check the code repository rather than the documentation, at least for the moment, until code is in official final stage and documentation gets fully updated. – Alvaro Commented Nov 28, 2018 at 20:43
1 Answer
Reset to default 3as @Alvero pointed out in the comments, instead of just supplying wp-api in the block registration, you now need to specify wp-api-fetch.
$index_js = 'sample-post/index.js';
wp_register_script(
'sample-post-block-block-editor',
plugins_url( $index_js, __FILE__ ),
array(
'wp-blocks',
'wp-i18n',
'wp-element',
'wp-api-fetch',
),
filemtime( "$dir/$index_js" )
);
Then within your block, you call it using the wp.apiFetch function:
var registerBlockType = wp.blocks.registerBlockType,
el = wp.element.createElement,
__ = wp.i18n.__,
apiFetch = wp.apiFetch;
const postSelections = [];
const allPosts = apiFetch({path: "/wp/v2/featured-post"}).then(fps => {
postSelections.push({label: "Select a Post", value: 0});
$.each( fps, function( key, val ) {
postSelections.push({label: val.title.rendered, value: val.id});
});
return postSelections;
});
本文标签: block editorUncaught TypeError wpapiFetch is not a function
版权声明:本文标题:block editor - Uncaught TypeError: wp.apiFetch is not a function 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749144216a2322718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp-api-fetchdependency in your script. – Alvaro Commented Nov 28, 2018 at 19:37'wp-api-fetch'and it should not throw the error. – Alvaro Commented Nov 28, 2018 at 20:24