Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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
Javascript 无法将第三方库链接到Sage主题WordPress中的js脚本_Javascript_Php_Wordpress_Trellis_Roots Sage - Fatal编程技术网

Javascript 无法将第三方库链接到Sage主题WordPress中的js脚本

Javascript 无法将第三方库链接到Sage主题WordPress中的js脚本,javascript,php,wordpress,trellis,roots-sage,Javascript,Php,Wordpress,Trellis,Roots Sage,我已经通过bower install-save Moment安装了一个名为Moment.js的第三方库。我有一个名为calendar.js的脚本放在我的主题的scripts文件夹中。然而,当我在calendar.js中使用矩时,库似乎没有链接到它。当我将所有代码从calendar.js移到main.js时,一切都正常。问题是我需要通过加载一个json文件将数据传递到脚本中,然后通过AJAX发送数据,因此我需要制作一个单独的脚本,在本例中是calendar.js。那么我该如何解决这个问题呢 编辑:

我已经通过bower install-save Moment安装了一个名为Moment.js的第三方库。我有一个名为calendar.js的脚本放在我的主题的scripts文件夹中。然而,当我在calendar.js中使用矩时,库似乎没有链接到它。当我将所有代码从calendar.js移到main.js时,一切都正常。问题是我需要通过加载一个json文件将数据传递到脚本中,然后通过AJAX发送数据,因此我需要制作一个单独的脚本,在本例中是calendar.js。那么我该如何解决这个问题呢

编辑:我已将calendar.js放入我的主题的functions.php中,如下所示:

function igs_scripts_styles() {
  wp_enqueue_script( 'calendar', get_template_directory_uri() . '/assets/scripts/calendar.js', array(), false, true );
}
add_action('wp_enqueue_scripts', 'igs_scripts_styles');

在wordpress中使用脚本之前,您需要先注册并排队,然后只有wordpress才能识别脚本。据我所知,main.js和Moment.js脚本是使用wp_enqueue_脚本正确添加的,这就是它们工作正常的原因。我相信,如果您将Calendar.js也放入队列,那么js文件也可以正常工作

这并不是这个问题的真正解决方案,而是一种变通方法。所以我注意到,当我将所有代码从calendar.js移到main.js时,一切都很好。我所做的是使用main.js的依赖项将calendar.js排队,因为moment.js在main.js中可用。代码如下:

 wp_enqueue_script( 'calendar', get_template_directory_uri() . '/assets/scripts/calendar.js', ['sage/js'], null, true );

我确实将calendar.js排队,main.js在wordpress默认设置中排队。Moment.js是通过bower-save安装的,因此应该可以自动使用:根据我从您所说的了解,您的calendar.js文件没有正确排队,您可以共享您的排队代码吗?如果您的calendar.js文件有jquery,您需要通过在数组中添加“jquery”来添加依赖项。请访问:了解更多信息排队脚本。您将在那里得到答案。我在calendar.js中使用jQuery没有任何问题