当我尝试将静态响应排入队列时,下面的代码出现无声故障。js脚本并将其注入div挂接到admin\\u页脚。我看到了页面和测试echo,以及脚本文件的url。但是React应用程序不在那里,基本上管理页面是空白的。
function prepAllAdminScripts() {
echo "<p>test echo prepAllAdminScripts</p>";
$url = PLUGIN_FOLDER_URL . \'shared/adminArea.bundle.js\';
echo "url: {$url}"; // shows working url to .js file
wp_register_script(
\'reactAdminArea\'
, $url
, null
, null
, true // false does not produce any change
);
wp_localize_script(\'reactAdminArea\', \'serverParams\', [
\'_wpnonce\' => wp_create_nonce(\'wp_rest\')
, \'apiBaseUrlFromWp\' => get_rest_url()
]);
// The non-wordpress way (below) works, loading the react.js script onto the admin page
// echo \'<div id="adminRoot"></div>\';
// echo "<script src=\'{$url}\'></script>";
}
function createInjectionDiv() {
echo \'<div id="adminRoot"></div>\';
}
add_action(\'admin_footer\', \'createInjectionDiv\');
add_action(\'admin_enqueue_scripts\', \'prepAllAdminScripts\');
function setupAdminSettingsPageParameters() {
add_options_page(
\'Plugin Settings\',
\'Plugin\',
\'manage_options\',
\'d_plugin\',
\'prepAllAdminScripts\'
);
}
add_action(\'admin_menu\', \'setupAdminSettingsPageParameters\');
我怀疑在我学习wordpress的过程中出现了一个新手错误,但似乎看不到。有人看到这个问题吗?
更新我已将以下内容添加到prepAllScripts()
wp_enqueue_script(\'reactAdminArea\');
// error
index.js:10 Uncaught Error: only one instance of babel-polyfill is allowed
at Object.eval (index.js:10)
at eval (index.js:29)
at Object.<anonymous> (adminArea.bundle.js:1)
at F (adminArea.bundle.js:1)
at r (adminArea.bundle.js:1)
at eval (index.jsx:1)
at Object.<anonymous> (adminArea.bundle.js:1)
at F (adminArea.bundle.js:1)
at adminArea.bundle.js:1
at adminArea.bundle.js:1
看起来babel polyfill正在加载两次。我通常会查看我的网页配置。但当我需要同样的反应时,这个问题并没有发生。直接使用js脚本(请参阅第一个发布的代码块中的注释代码)