我有一个Grails (2.3.6)应用程序,并将其配置为使用asset-pipeline
// BuildConfig.groovy
plugins {
compile ":asset-pipeline:1.8.7"
}在我的grails-app/assets/javascripts目录中,我有:
myapp.jsfizz.jsbuzz.js然后在我的grails-app/views/index.gsp里
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" type="text/css" href="${resource(dir: 'css', file: 'myapp.css')}" />
</head>
<body>
<div id="home" class="page">
<!-- Content... -->
</div>
<asset:javascript src="myapp.js" />
</body>
</html>最后,在我的myapp.js
= require fizz.js
= require buzz.js
= require_self
// INITIALIZE THE APPLICATION.
init();
function init() {
alert("Do stuff!");
}当我做grails run-app时,应用程序就会启动得很好。当我去查看页面源(对于index.gsp)时,我得到:
<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" type="text/css" href="/static/css/myapp.css" />
</head>
<body>
<div id="home" class="page">
<!-- Content... -->
</div>
<script src="/assets/myapp.js?compile=false" type="text/javascript" ></script>
</body>
</html>但是,当我点击/assets/myapp.js?compile=false链接查看它的源代码时,我得到了与上面完全相同的JS:
= require fizz.js
= require buzz.js
= require_self
// INITIALIZE THE APPLICATION.
init();
function init() {
alert("Do stuff!");
}因此,asset-pipeline不仅没有在myapp.js顶部翻译这些require语句,也没有提取指定的JS文件,而且它也没有去掉require。如何配置插件以正确地插入其他JS库(fizz.js和buzz.js)?
发布于 2014-05-31 13:36:27
在使用资产管道时,需要与require指令一起使用适当的语法。
这就是myapp.js应该从(没有空的第一行)开始的方式:
//= require fizz.js
//= require buzz.js
//= require_self详细参考使用文件。
发布于 2014-09-27 23:13:37
在实现#dmahapatro回答后,您的页面源应该自动显示
!DOCTYPE html>
<html>
<head>
<title>My App</title>
<link rel="stylesheet" type="text/css" href="/static/css/myapp.css" />
</head>
<body>
<div id="home" class="page">
<!-- Content... -->
</div>
<script src="/assets/myapp.js?compile=false" type="text/javascript" ></script><script src="/assets/fizz.js?compile=false" type="text/javascript" ></script><script src="/assets/buzz.js?compile=false" type="text/javascript" ></script>
</body>
</html>这个代码片段
/*
*= require fizz.js
*/告诉make资产管道在打开应用程序上的页面时,将fizz.js文件作为链接脚本添加到头中
这也适用于css文件(视情况而定)。
https://stackoverflow.com/questions/23969034
复制相似问题