Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress中的JavaScript-范围问题_Javascript_Wordpress_Scope - Fatal编程技术网

Wordpress中的JavaScript-范围问题

Wordpress中的JavaScript-范围问题,javascript,wordpress,scope,Javascript,Wordpress,Scope,我不明白为什么我的基本JavaScript不能在安装了Wordpress的本地服务器上运行 我在main.js中有这个,它被绑定到bundle.js: const test = 'this is a test'; console.log(test); 在my functions.php中,我将脚本排入队列: wp_enqueue_script( 'customjs', get_template_directory_uri() . '/dist/js/bundle.js' , array('jq

我不明白为什么我的基本JavaScript不能在安装了Wordpress的本地服务器上运行

我在main.js中有这个,它被绑定到bundle.js:

const test = 'this is a test';
console.log(test);
在my functions.php中,我将脚本排入队列:

wp_enqueue_script( 'customjs', get_template_directory_uri() . '/dist/js/bundle.js' , array('jquery'), '1.0', true );
一旦我加载了站点,测试就会被记录下来,我会得到“这是一个测试”。但是当我想访问控制台中的变量测试时,我得到一个引用错误:

Uncaught ReferenceError: test is not defined
at <anonymous>:1:1
(anonymous) @ VM17626:1
未捕获引用错误:未定义测试
时间:1:1
(匿名)@VM17626:1

我错过了什么?我试着寻找答案,但这太简单了,我找不到任何答案。当全局声明test时,如何获得引用错误?

通常当我看到
bundle.js
时,这意味着它是用Babel/Webpack等编译的,是这样吗?如果是这样,您使用模块和
const test
,将不会创建全局变量。谢谢,我不知道这一点。我对这些打包机不熟悉。我刚刚尝试导入一个模块,该模块声明一个变量,然后记录它。它可以工作,但我仍然无法访问控制台中的变量。这意味着当使用绑定器等时,没有全局变量?一般来说,全局变量是您想要尝试和避免的,但是如果您想说向全局公开一个变量,您可以执行类似->
window.test=test
的操作,非常感谢您让我明白了所有符号,干杯