在Play Framework WebJars示例应用程序中使用jQuery

在Play Framework WebJars示例应用程序中使用jQuery,jquery,playframework,requirejs,webjars,Jquery,Playframework,Requirejs,Webjars,我正在使用和试用。我查看了应用程序模板并阅读了Bootstrap依赖于jQuery的内容。因此,当您将引导指定为依赖项时,也会得到jQuery。在测试页面中 好的,我想我现在有了jQuery——让我们尝试一下,在index.scala.html中添加以下最小代码: 现在,我从Safari中得到以下错误: [Error] Failed to load resource: the server responded with a status of 404 (Not Found) (jquery.js

我正在使用和试用。我查看了应用程序模板并阅读了Bootstrap依赖于jQuery的内容。因此,当您将引导指定为依赖项时,也会得到jQuery。在测试页面中

好的,我想我现在有了jQuery——让我们尝试一下,在index.scala.html中添加以下最小代码:

现在,我从Safari中得到以下错误:

[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (jquery.js, line 0)
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (jquery.js, line 0)
[Error] Error: Script error for: jquery
http://requirejs.org/docs/errors.html#scripterror
最后,我在app/assets中打开index.coffee脚本并在此处尝试:

require ["bootstrap"], () ->
  console.log "boostrap javascript loaded"
  $('.container').hide()

终于胜利了!但是把我所有的jQuery代码都写在一个外部文件中真的很讨厌。如何使jQuery在HTML模板中工作?如何封装代码,使其仅在加载了RequireJS之后执行?谢谢

使用WebJAR进行依赖关系管理将把可传递的依赖关系拉到项目中,但这并不意味着它们会神奇地加载到页面中。因此,如果要使用jQuery,则需要加载它。您可以使用WebJars中的RequireJS支持来加载发现的可传递依赖项。如果您只想将jQuery加载到index.scala.html页面,请执行以下操作:

<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>

WebJars文档中的更多详细信息:

使用WebJars进行依赖关系管理将把可传递的依赖关系拉到项目中,但这并不意味着它们会神奇地加载到页面中。因此,如果要使用jQuery,则需要加载它。您可以使用WebJars中的RequireJS支持来加载发现的可传递依赖项。如果您只想将jQuery加载到index.scala.html页面,请执行以下操作:

<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
WebJars文档中的更多详细信息:

<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>