Ruby-on-rails application.css.scss 中的 Require 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26171159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:57:01  来源:igfitidea点击:

Require statement in application.css.scss

ruby-on-railssassfancyboxcolorbox

提问by user2511030

I want to use a lightbox gem such as fancybox or color box. Both gems ask to add this line in the application.css

我想使用灯箱宝石,例如fancybox 或color box。两个 gem 都要求在 application.css 中添加这一行

 *= require colorbox-rails

Here's the problem. I only have application.css.scss files. All my css files are scss files. I have import statements in the application.css.scss but no *=require statements. Adding the above line results in the error :

这就是问题所在。我只有 application.css.scss 文件。我所有的 css 文件都是 scss 文件。我在 application.css.scss 中有 import 语句,但没有 *=require 语句。添加上面的行会导致错误:

Invalid CSS after "*": expected "{", was "=require colorb..."

“*”之后的 CSS 无效:预期为“{”,是“=require colorb...”

Here's the complete application.css.scss

这是完整的 application.css.scss

@import "bootstrap";
@import "welcome";
@import "sessions";
@import "users";


*= require colorbox-rails

回答by joshua.paling

application.css.scssor application.cssare kinda the same. Just rename your application.cssto application.css.scss.

application.css.scss或者application.css有点相同。只需将您的重命名application.cssapplication.css.scss.

As for adding that line, it'll need to be right up the top, in a comment. Like this:

至于添加该行,它需要在评论中位于顶部。像这样:

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, 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 bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_self
 *= require colorbox-rails
 */

@import "bootstrap";
@import "welcome";
@import "sessions";
@import "users";

回答by jjk

Explicitly, I had this issue with fonts files declaring different font-faces using a single font.scss file in my stylesheets directory (Rails app using sass and haml). I had followed related issues (like this article on SO) and had tried a number of related solutions like removing '-' from font ttf filename, changing the url declarations in the font.scss file, etc.

明确地,我在使用样式表目录(使用 sass 和 haml 的 Rails 应用程序)中的单个 font.scss 文件声明不同字体的字体文件时遇到了这个问题。我关注了相关问题(如SO 上的这篇文章)并尝试了许多相关解决方案,例如从字体 ttf 文件名中删除“-”、更改 font.scss 文件中的 url 声明等。

@joshua.paling's solution worked for me ...

@joshua.paling 的解决方案对我有用......

  1. Change the font.scss file name to font.css
  2. utilize the following declaration style in the application.scss file:

    /* ...    
    *= require_self    
    *= require fonts    
    */
    # @import other files here excluding the font.css file
    
  1. 将 font.scss 文件名更改为 font.css
  2. 在 application.scss 文件中使用以下声明样式:

    /* ...    
    *= require_self    
    *= require fonts    
    */
    # @import other files here excluding the font.css file
    

And with this configuration the following font-face declarations still worked

使用此配置,以下字体声明仍然有效

 @font-face {
   font-family: PT Serif;
   src: url('PT_SerifWebRegular.ttf') format("truetype");
 }