jQuery 插件和 JS 脚本之间的 Grails 冲突

Grails conflict between jQuery Plugin and JS script

我有一个需要 jQuery 的 Grails 应用程序,所以我通过插件加载 jQuery 和 jQuery-UI:

buildConfig.groovy:
    runtime ":jquery:1.11.1"
    compile ":jquery-ui:1.10.4"

只要我不包括也使用 jQuery 的独立 JS 脚本,它就可以正常工作。这个脚本被命名为 myscript.jquery.js 我把它放在 web-app/js.

现在我将这些插件和脚本包含在我的 main.gsp:

main.gsp:
<html>
<head>
      ...

<r:require modules='jquery, jquery-ui, bootstrap, customcss' />
<g:javascript src="myscript.jquery.js"/>

<r:layoutResources disposition="defer" />

<g:layoutHead/>
<r:layoutResources disposition="head"/>

</head>
<body>
      ...

我得到

ReferenceError: jQuery is not defined
$ = jQuery;

我是否犯了一个特定的错误? 有没有更好的方法来处理这个问题?

PS:我试图通过 main.gsp 中的脚本直接加载 jQuery,但没有成功,因为我有其他依赖于 jQuery 的插件]插件:-/

将您的脚本添加到配置中的 ApplicationResources.groovy 文件中,然后将该模块添加到页面中。这将解决问题。

示例: 在你的 conf/ApplicationResources.groovy

modules = {
    baseJS {
        resource url: 'js/myscript.jquery.js'
    }
}

在您的 gsp 文件中

<r:require modules='jquery, jquery-ui, bootstrap, customcss, baseJS' />

这是旧博客,但您会明白的

http://www.anyware.co.uk/2005/2011/09/12/optimising-your-application-with-grails-resources-plugin/

谢谢