为什么 Sass 找不到我的颜色查找映射的元素?

Why can't Sass find the element of my color lookup mapping?

我不明白为什么 map-get 没有 return 预期的结果:

$buttonColors: (blue: lighten(blue, 25%), grey: lighten(gray, 40%));

@debug map-get($buttonColors, 'blue');

出于某种原因,这是 returning null,而我期待它 return #8080ff。这会导致我的代码进一步出现问题,因为我无法将空值传递给 lighten 或 darken 等函数。

当您告诉 Sass 查找其键为字符串的元素时,您的映射为其键使用颜色。颜色 blue 与字符串 'blue' 不同。结果,查找失败,map-get() 函数 returns NULL。 HTML/CSS 规范中定义的所有颜色关键字都具有 color 类型,除非您引用它们(或通过插值将它们转换为字符串)。

您可以停止将 map-get 的第二个参数转换为字符串,但如果您只是切换到始终使用字符串作为映射键会更好(使用其他任何东西只会导致混淆 and/or错误)。

$buttonColors: ('blue': lighten(blue, 25%), 'grey': lighten(gray, 40%));