我如何将嵌套的sass映射分离为新地图?例如,我有这样的Sass地图:
$susy-setting: (
s: (
'columns': 4,
'gutters': 30px,
),
m: (
'columns': 8,
'gutters': 30px,
),
l: (
'columns': 12,
'gutters': 30px,
)
);
然后,我需要在循环之后隔离每个地图,因为我的其他混音需要其参数。
@each $setting in $susy-setting{
@include susy-settings-block($setting) { // This mixin need map
@for $i from 1 through map-get($setting, 'columns') {
@content;
}
}
}
要回答您的主要问题,您需要在循环中同时获得键和值:@each $size, $setting in $susy-setting
。$size
变量将存储键(例如s
,m
,l
(,而$setting
变量存储分配给该密钥的地图值。
编辑:我看到了您对要点的评论,这提供了更多的上下文。尝试以下操作:
@mixin susy-settings-block(
$name,
$config: $susy-settings
) {
// store the old settings
$global: $susy;
// Get the new settings by name
$new: map-get($config, $name);
// apply the new settings
$susy: map-merge($susy, $new) !global;
// allow nested styles, using new settings
@content;
// return to the initial settings
$susy: $global !global;
}