【问题标题】:Asset tag on grails 2.4.0grails 2.4.0 上的资产标签
【发布时间】:2014-06-20 06:27:59
【问题描述】:

我在 2.4.0 版本上开始了一个新的 grails 项目。 在这个版本中,grails-app 文件夹中添加了子文件夹asset,以便于访问js、css、图片等。

但是,我无法访问子文件夹,例如,我的文件夹结构是这样的:

grails-app/assets/stylesheets/fonts/font.css

然后当我使用 <asset:stylesheet src="font.css"/>(注意 font.css 放在 stylesheets 文件夹中)

但是当我尝试这个时,没有找到 font.css。

<asset:stylesheet src="/fonts/font.css"/>

请建议另一种使用资产标签访问子文件夹的方法。

【问题讨论】:

  • 您的样式表标题是什么样的?是否包括= require self

标签: grails


【解决方案1】:

不要直接链接到子文件夹。在grails-app/assets/stylesheets 中使用application.css

application.css 中,通过添加以下内容告诉资产插件包含您的font.css

/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS file within this directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require fonts/font.css
*= require self
*/

在您的布局 (GSP) 中,然后像这样使用 application.css

<asset:stylesheet src="application.css"/>

这将解决您的问题。

【讨论】:

  • 虽然你提到的聚合样式通常是个好主意,但 OP 采用的方法是有效的,应该在客户端生成一个 CSS 文件.在每个页面上包含所有 CSS 通常是矫枉过正的,因此在需要的地方加载模块是一种明智的方法。恕我直言,如果您想聚合 CSS 并将其拆分,那么您已经迫切需要 SASS 或 LESS ;)
  • 嗯,它需要像 stylesheets/fonts/font.css 这样的子文件夹吗?我尝试了上面的解决方案,但它不起作用。
【解决方案2】:

您采用的方法应该奏效。我刚刚测试为 vanilla 2.4.0 项目并且只添加了上面的标签 - 它在 OSX 上运行良好。我能看到的唯一不完全标准的事情是你不需要在fonts/font.css 之前领先/

我会尝试删除它,然后仔细检查任何地方都没有错字。您还可以使用控制台(它会抱怨缺少资产)和 Chrome 开发者工具来调试它请求的地址(在“网络”选项卡中,您应该会看到带有 404 错误的资产)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    相关资源
    最近更新 更多