简体   繁体   English

如何使用 Sass 变量和 mixins 与反应 js

[英]how to use Sass variables and mixins with react js

As I'm using same set of code in different components.因为我在不同的组件中使用相同的代码集。 Here is my code structure.这是我的代码结构。

在此处输入图像描述

I want to use Sass Variables and mixins so that i don't have to repeat the Same code in different files many times.我想使用Sass 变量和 mixins ,这样我就不必在不同的文件中多次重复相同的代码。 Here is my Variables file.这是我的变量文件。 And I don't know how to use these colors in different files in components.而且我不知道如何在组件的不同文件中使用这些 colors。

在此处输入图像描述

And There are many buttons which are using same css.并且有许多按钮使用相同的 css。 How to create one mixin and use it in different places.如何创建一个 mixin 并在不同的地方使用它。

You can import your variables.scss file in any of your other scss files and use the variables.您可以在任何其他 scss 文件中导入 variables.scss 文件并使用这些变量。 Here is an example:-这是一个例子:-

@import '../variables.scss'; // you have to give the path here according to your folder structure

.button {
  color: $main-color;
}

You can use mixins too in a similar way.您也可以以类似的方式使用 mixins。 You can put all your mixins in a file named mixins.scss.你可以将所有的 mixins 放在一个名为 mixins.scss 的文件中。

@mixin btn($color) {
  color: $color;
}

and in your component scss file, you can import it and use it在您的组件 scss 文件中,您可以导入并使用它

@import '../mixins.scss';
@import '../variables.scss';

.button {
  @include btn($main-color);
}

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

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