简体   繁体   English

SCSS / SASS在文件之间共享变量,混入和样式,而无需导入

[英]SCSS/SASS Share variables, mixins & styles between files without importing

By using @import method in SCSS/SASS i can compile a big .CSS file out of my different chunks of .SCSS files (eg. _color.scss, _var.scss, styles.css ...) , but if it possible to have different chunks of .CSS files which share values between themselves but do not add same styles when compiled? 通过在SCSS / SASS中使用@import方法,我可以从我的.SCSS文件的不同块(eg. _color.scss, _var.scss, styles.css ...)编译出一个大的.CSS文件(eg. _color.scss, _var.scss, styles.css ...)有不同的.CSS文件块,它们之间共享值,但在编译时不添加相同的样式?

Eg: make menu.css using _color.scss, _var.scss, _grid.scss and menu.scss 例如:让menu.css使用_color.scss, _var.scss, _grid.scss and menu.scss
and make appBar.css using _color.scss, _grid.scss and appBar.scss appBar.css使用_color.scss, _grid.scssappBar.scss

I want to use both the files in same page/project which loads when required, 我想使用需要时加载的同一页面/项目中的两个文件,
but the problem is i if i use @import in SCSS/SASS it will import all the styles and vars form that import file and create a .CSS file for me and both the files will have same property repeated again and again in between files. 但是问题是如果我在SCSS / SASS中使用@import,它将导入所有导入文件的样式和vars形式,并为我创建一个.CSS文件,并且两个文件在两个文件之间具有相同的属性,并会重复一次。

You can use @mixins, @functions and @extends rather than import all the CSS. 您可以使用@ mixins,@ functions和@extends而不是导入所有CSS。

For example, if you have this in your _color.scss 例如,如果您的_color.scss中有此文件

$app-colors: {
  color-black: #000,
  color-white: #fff
}

@function getColor( $color-name) {
  @return map-get( $app-colors, $color-name );
}

You only need to use the getColor function in your final SASS and the code will not be repeated again and again. 您只需要在最终的SASS中使用getColor函数,该代码就不会一次又一次地重复。

So, in your common SCSS files you will only have functions or methods but no classes. 因此,在普通的SCSS文件中,您将仅具有函数或方法,而没有类。 The classes will be generated using these methods in the final SCSS files. 这些类将在最终的SCSS文件中使用这些方法生成。

However, I'm not so sure about your need of have fully isolated CSS chunks like menu.css and appbar.css that work fully perfect without the help of others css files. 但是,我不确定您是否需要完全隔离的CSS块(例如menu.css和appbar.css),它们在没有其他CSS文件帮助的情况下可以完全完美地工作。 It is quite weird. 这很奇怪。

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

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