gpt4 book ai didi

sass - "Not a map for ` 向映射添加新元素后 map-get `"错误

转载 作者:行者123 更新时间:2023-12-02 01:42:55 25 4
gpt4 key购买 nike

如何将映射合并到 sass 中的现有映射中?

我目前在 _config 部分中使用 sass 映射来定义我的断点,例如:

$breakpoints: (
small: 35rem,
medium: 55rem,
large: 75rem,
xlarge: 90rem,
element-breakpoint-1: 100rem,
element-breakpoint-2: 110rem
);

@mixin breakpoint($width) {
@media screen and (min-width: map-get($breakpoints, $width)) {
@content;
}
}

.element {
width: 100px;
@include breakpoint(element-breakpoint-1) {
width: 200px;
}
@include breakpoint(element-breakpoint-2) {
width: 300px;
}
}

理想情况下,我希望能够向现有映射添加新断点:

@function array-append($list, $value) {
@return join($list, $value);
}

$breakpoints: array-append($breakpoints, (element-breakpoint-1: 100rem, element-breakpoint-1: 110rem));

问题是断点混合在 $breakpoints 列表中看不到新值,SASS 抛出以下错误:

Error: $map: (("small" 35rem), ("medium" 55rem), ("large" 75rem), ("xlarge" 90rem), ("element-breakpoint-1" 100rem)) is not a map for `map-get'

最佳答案

您要查找的函数是map-merge(),而不是join()join() 函数用于将列表连接在一起,并导致您的映射转换为列表列表。

$fix-mqs: false;
$breakpoints: (
small: 35rem,
medium: 55rem,
large: 75rem,
xlarge: 90rem
);

// map-merge here, not join
$breakpoints: map-merge($breakpoints, (element-breakpoint-1: 100rem, element-breakpoint-2: 110rem));

@mixin breakpoint($width) {
@if $fix-mqs {
@if $fix-mqs >= map-get($breakpoints, $width) {
@content;
}
}
@else {
@media screen and (min-width: map-get($breakpoints, $width)) {
@content;
}
}
}

@include breakpoint(element-breakpoint-2) {
.foo {
color: red;
}
}

输出:

@media screen and (min-width: 110rem) {
.foo {
color: red;
}
}

关于sass - "Not a map for ` 向映射添加新元素后 map-get `"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27413854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com