简体   繁体   English

如何导入部分 scss 文件

[英]How to import part of an scss file

For example I am trying to import .navbar-nav from bootstrap's _navbar.scss and not the whole _navbar.scss file to my compiled css file.例如,我试图将.navbar-nav从引导程序的_navbar.scss而不是整个_navbar.scss文件导入到我编译的 css 文件中。 Is there a way to do it?有没有办法做到这一点?

Sorry if this was asked before.抱歉,如果之前有人问过这个问题。

You can try doing an extend:您可以尝试进行扩展:

.your-class{
    @extend .navbar-nav;
}

However, this would only work if you had imported the _navbar.scss somewhere else or the bootstrap.scss.但是,这只有在您将 _navbar.scss 导入其他地方或 bootstrap.scss 时才有效。

Additional额外的

// main.scss
@import ../wherever bootstrap file is/_navbar.scss;
@import _custom.scss;

// _custom.scss
.your-class{
    @extend .navbar-nav;
}

One of the way to import.scss in javascript is javascript 中 import.scss 的一种方法是

import { navbar-nav } from '_navbar.scss'

When using in your component you can do.在您的组件中使用时,您可以这样做。

<div className={navbar-nav} />

if you want to import it in your.scss file then you can do.如果你想将它导入到你的 .scss 文件中,那么你可以这样做。

@import '_navbar.scss'

.class {
  @extend .navbar-nav
}

As you are learning Sass here are some explanations which may help:在您学习 Sass 时,这里有一些可能会有所帮助的解释:

Better wording helps...更好的措辞有助于...

At first some wording to get a correct understandable communication here and anywhere else you are talking about coding:首先,一些措辞可以在这里和您谈论编码的任何其他地方获得正确的可理解的沟通:

  1. SASS don't minify a given CSS, it writes the CSS. SASS 不要minify给定的 CSS,它会写入 CSS。 Minify means the process that a given CSS code is compressed by a postprocessor to a shorter way to write it, - ie comments and spaces will be removed... But yes: as SASS writes CSS it is able to write code in a minified format.缩小意味着给定 CSS 代码被后处理器压缩为更短的编写方式的过程 - 即注释和空格将被删除......但是是的:当 SASS 以 a8DFD293172F4D2 格式编写 Z2C56C360580420D293172F4 代码时.

  2. What you mean is to 'reduce code' or 'avoid not needed code' as you only try to import, use and write.您的意思是“减少代码”或“避免不需要的代码”,因为您只尝试导入、使用和编写。 the only needed parts of a given module which is a good practice.给定模块中唯一需要的部分,这是一个很好的做法。

  3. .navbar is a CSS class. .navbar是 CSS class。 SASS don't load CSS classes, it writes CSS classes. SASS 不加载 CSS 类,它写入 CSS 类。 It doesn't matter if you 'write the code on your own to a SCSS file' or 'get the code from a framework/module'... SASS writes the however prepared CSS classes to your CSS file.无论您是“将代码自己编写到 SCSS 文件”还是“从框架/模块获取代码”...

  4. What you mean is the SASS includes/imports files with code from a framework/module to write that code/classes to css.您的意思是 SASS 包含/导入带有来自框架/模块的代码的文件,以将该代码/类写入 css。 So yes: maybe you can say you 'load' that module/scss-file... but you don't load as css class.所以是的:也许你可以说你“加载”了那个模块/scss文件......但你没有加载为css class。 (This is as important as 'classes' in coding allways means a special construct of excutable code which does something in your programm. CSS classes don't execute anything, in SASS they are content you want to write/output to css.) (这与编码中的“类”一样重要,始终意味着可执行代码的特殊构造,它在您的程序中执行某些操作。CSS 类不执行任何操作,在 SASS 它们是您想要写入/输出到 ZC7A628CBA16AE2A28EB6 的内容)

Please: these wordings are important to understand each other and to understand the mechanic of the process how SASS works is going on as well.拜托:这些措辞对于相互理解和理解 SASS 工作的过程机制很重要。

Reducing code by importing only selected file is good practice通过仅导入选定的文件来减少代码是一种很好的做法

So, I am not sure if I did understand your question right:所以,我不确定我是否正确理解了你的问题:

  1. No. You are not able to include/import/load a part of the code of a single scss-file only.不可以。您不能仅包含/导入/加载单个 scss 文件的部分代码。 If you do @import 'somefile.scss' you always get the whole code of the whole file.如果你这样做@import 'somefile.scss'你总是得到整个文件的整个代码。
  2. Yes.是的。 you are able to include/import/load parts of a given framework/module as you are able to load only the special FILES(.) of a framework/module you need for your project.您可以包含/导入/加载给定框架/模块的部分内容,因为您只能加载项目所需的框架/模块的特殊 FILES(.)。
  3. Yes.是的。 That is a really good practice.这是一个非常好的做法。

As you mentioned Bootstrap indeed is developed and allows you to do that.正如您提到的,Bootstrap 确实已经开发并允许您这样做。 But head up.但是抬头。 If you import ie the part navbar.scss (or other selected elements) it only works if you also load the other files navbar.scss depends on.如果您导入即部分navbar.scss (或其他选定元素),则它仅在您还加载其他文件navbar.scss依赖时才有效。 That are almost variables , functions , mixins and sometimes needed JS components to this element as well.这几乎是variablesfunctionsmixins ,有时还需要该元素的JS components Please note, that importing the files the elements are based on (ie vars, functions, mixins) has to be done BEFORE you load the element (ie like navbars, grid,...) itself.请注意,导入元素所基于的文件(即变量、函数、mixins)必须在加载元素(即导航栏、网格等)之前完成。

A way to organize your project一种组织项目的方法

Yes.是的。 A good way to organize your project is to have a single(.!!) file which brings all the code together you write in other partial files yourself or which you import from other framework/modules.组织项目的一个好方法是拥有一个(.!!)文件,它将您自己在其他部分文件中编写的所有代码或您从其他框架/模块导入的所有代码组合在一起。

In case of Bootstrap this can be (simplified example):在 Bootstrap 的情况下,这可以是(简化示例):


// ###> file: your 'custom.scss'

// Note: file is without leading underscore 
// as this files GENERATES/WRITE the css to custom.css
// Files with underscore as _partial-footer-styling.scss 
// are not compiled to write css on their own
// that files are only compiled to css when they are imported to files without underscore


@import 'path/your-own-vars';
// Note: technique importing files 
// you don't need to write underscore and '.scss'
// Note: function of this file
// the file '_your-own-vars.scss' is to organize you needed vars special to your project
// it includes your own vars and bootstrap vars as well
// --> the Bootstrap vars in this file will overwrite the vars of Bootstrap which will be included next

@import 'bootstrap-path/functions';
@import 'bootstrap-path/variables';
@import 'bootstrap-path/mixins';

@import 'bootstrap-path/your-selected-component-1';
@import 'bootstrap-path/your-selected-component-2';
@import 'bootstrap-path/your-selected-component-3';
...

@import 'path/partial-your-own-additional-css-special-section';
@import 'path/partial-your-own-additional-css-footer-settings';
....


A detailed explanation how to include and use Bootstrap (partly if you like to do so) to your project is here: https://getbootstrap.com/docs/4.6/getting-started/theming/如何在您的项目中包含和使用 Bootstrap(如果您愿意的话,部分是这样)的详细说明如下: https://getbootstrap.com/docs/4.6/getting-started/theming/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM