检查是否在SASS中定义了变量

红宝石 CSS

神乐Harry

2020-03-19

如标题所示,我正在尝试检查是否在SASS中定义了变量。(如果有任何不同,我正在使用指南针)

我发现了Ruby等效项:

defined? foo

给了我黑暗中的一枪,但这只是给了我错误:

defined": expected "{", was "?

我找到了解决方法(显然只是在所有情况下都定义了变量,在这种情况下实际上更有意义),但我真的很想知道将来是否有可能

第2223篇《检查是否在SASS中定义了变量》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
老丝猿阿飞 2020.03.19

Just as a complementary answer - you should have a look on the default keyword for certain use cases. It gives you the possibility to assign a default value to variables in case they are not defined yet.

You can assign to variables if they aren’t already assigned by adding the !default flag to the end of the value. This means that if the variable has already been assigned to, it won’t be re-assigned, but if it doesn’t have a value yet, it will be given one.

Example:

In specific-variables.scss you have:

$brand: "My Awesome Brand";

In default-variables.scss you have:

$brand: company-name !default;
$brand-color: #0074BE !default;

Your project is built like this:

@import "specific-variables.scss"; @import "default-variables.scss"; @import "style.scss";

The value of brand will be My Awesome Brand and the value of brand color will be #0074BE.

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android