You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法使用@use导入SCSS变量文件,寻求解决方法

问题:使用@use导入含变量的SCSS文件失败

尝试用@use引入包含变量的_variables.scss文件时失败,但导入不含变量的其他SCSS文件正常。

_variables.scss 代码

$soft-color: #e1e8f0;
$text-hero: white;
$text-white: white;
$card-color: #162c5b;
$button-color: white;
$button-text-color: black;
$features-color: #5ccaa7;
$technology-color: #038aff;
$awards-color: #038aff;
$easy-color: #febd3d;

$small-font: 0.8rem;

// Typography

//New variables
$ff-text: "Inter", sans-serif;
$fw-400: 400;
$fw-500: 500;
$fw-600: 600;
$fw-700: 700;
$box-shadow: rgba(35, 24, 54, 0.25);

/*.   Mixins.   */

@mixin shadow {
  border-radius: 12px;
  border: 1px solid #e8ebf2;
  box-shadow: rgba(0, 0, 0, 0.06) 0px 1px 4px;
}

/*    Functions    */

@function color($elem, $variant) {
  @return map-get(map-get($color, $elem), $variant);
}

/*    Variables   */

$color: (
  text: (
    1: #162c5b,
    2: #565678,
    3: #ababc9,
    soft: #718097,
    footer: #acaabf,
    1i: #fff,
    2i: rgba(#fff, 0.4),
  ),
  bg: (
    hero: #038aff,
    awards: #fafbff,
    3: #d2daf0,
    infinite: #161348,
    footer: #161348,
    line-footer: #2f2c5b,
    white: #fff,
  ),
);

主SCSS文件代码

@use "header";
@use "layout";
@use "variables";


/*  Hero section  */
.hero {
  width: 100%;
  position: relative;
  background-color: color(bg, hero);
  &::before {
    content: "";
    position: absolute;
    left: 0;
    bottom: -70px; /* Keep a fixed pixel value */
    width: 100vw;
    height: 70px;
    z-index: 10;
    background: linear-gradient(
      to right bottom,
      color(bg, hero) 50%,
      transparent 50%
    );
  }
  .container {
    width: min(1140px, 100%);
    margin: 0 auto;
    .flexbox {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      width: 100%;
      min-height: 700px;
      .box {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        padding: 1rem;
        &:nth-child(2) {
          justify-content: center;
        }
        .trust {
          display: flex;
          flex-wrap: wrap;
          align-items: center;
          .logos {
            display: flex;
            flex-wrap: wrap;
            .logo {
              width: 110px;
              padding: 0 10px;
            }
          }
        }

        .card-wrapper {
          width: 440px;
          height: 560px;
          padding: 28px;
          background-color: white;
          border-radius: 16px 16px 80px 20px;
          .card-info {
            .top-info {
              display: flex;
              flex-wrap: wrap;
              justify-content: space-between;
              align-items: center;
              padding: 0 0 20px 0;
              .addnew {
                font-size: small;
                padding: 4px 8px;
                border-radius: 6px;
                border: 1px solid color(text, soft);
              }
              h3 {
                color: color(text, 1);
              }
              a {
                color: color(text, 1);
              }
            }
            .content {
              width: 100%;
              .box {
                display: flex;
                flex-wrap: wrap;
                justify-content: space-between;
                padding: 20px 0 20px 0;
                border-bottom: 1px solid $soft-color;
                .place {
                  h3 {
                    color: color(text, 1);
                    padding-bottom: 6px;
                  }
                  p {
                    color: color(text, soft);
                    font-size: small;
                  }
                }
              }
            }
            .card-image {
              position: relative;
              overflow: hidden;
              width: 100%;
              height: 250px;
              padding: 24px;
              border-radius: 12px;
              background-color: $card-color;
              box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
              &::before {
                content: "";
                position: absolute;
                top: 0;
                right: -130px;
                width: 400px;
                min-height: 300px;
                background-image: url("../img/world_map.svg");
                background-repeat: no-repeat;
                z-index: 0;
              }
              .info {
                position: relative;
                z-index: 10;
                .number {
                  h3 {
                    font-size: 0.7rem;
                    text-transform: uppercase;
                    padding-bottom: 10px;
                  }
                  h2 {
                    font-size: 1.5rem;
                  }
                }
                .details {
                  width: 100%;
                  .flex {
                    display: flex;
                    flex-wrap: wrap;
                    div {
                      flex: 1;
                      h3 {
                        color: $text-white;
                        font-size: 0.7rem;
                        padding-bottom: 6px;
                        text-transform: uppercase;
                      }
                      h4 {
                        color: $text-white;
                        font-size: 0.95rem;
                        font-weight: $fw-400;
                      }
                    }
                  }
                }
              }
              .iconbar {
                position: relative;
                z-index: 10;
                display: flex;
                flex-wrap: wrap;
                align-items: center;
                justify-content: space-between;
                padding-bottom: 30px;
                .mastercard {
                  width: 40px;
                }
                .active {
                  padding: 6px 8px;
                  border-radius: 4px;
                  background-color: #79c7a9;
                  p {
                    color: $text-hero;
                    font-size: 0.8rem;
                  }
                }
              }
            }
          }
        }
        .barra {
          width: 100%;
          padding-bottom: 30px;
          .button {
            display: inline-block;
            padding: 14px 18px;
            border-radius: 6px;
            color: $button-text-color;
            background-color: $button-color;
            i {
              padding-left: 10px;
            }
          }
        }

        h1,
        h2,
        h3 {
          color: $text-hero;
          line-height: 1.13;
        }
        h1 {
          font-weight: 600;
          font-size: clamp(2rem, 5vw, 3.8rem);
          padding-bottom: 30px;
        }
        h2 {
          font-weight: $fw-400;
          font-size: clamp(1rem, 3vw, 1.3rem);
          line-height: 1.5;
          padding-bottom: 30px;
        }
        h3 {
          font-weight: $fw-400;
          font-size: 0.9rem;
        }
      }
    }
    @media screen and (max-width: 480px) {
      .flexbox {
        .box {
          flex: 1 0 100%;
          .card-wrapper {
            max-width: 440px;
            width: 100%;
            height: auto;
          }
        }
      }
    }
  }
}

文件结构

文件结构截图

解决方案

问题出在@use的核心规范上:和旧的@import不同,@use导入的模块中,变量、混合宏、函数必须通过命名空间访问(默认命名空间是文件名去掉下划线和后缀)。你的代码直接使用$soft-color、color()等标识符,SCSS无法定位到来源模块,导致导入失效。

具体修复步骤:

  1. 变量前添加命名空间:
    把$soft-color改为variables.$soft-color,$card-color改为variables.$card-color,所有来自_variables.scss的变量都要加上variables.前缀。

  2. 函数、混合宏前添加命名空间:
    把color(bg, hero)改为variables.color(bg, hero);如果使用混合宏@include shadow(),要改为@include variables.shadow()。

  3. (可选)自定义简化命名空间:
    可以在导入时指定别名,减少代码长度:

    @use "variables" as v;
    

    之后就可以用v.$soft-color、v.color(bg, hero)来访问模块内容。

修复后的主SCSS片段示例:

@use "header";
@use "layout";
@use "variables";

/*  Hero section  */
.hero {
  width: 100%;
  position: relative;
  background-color: variables.color(bg, hero);
  &::before {
    background: linear-gradient(
      to right bottom,
      variables.color(bg, hero) 50%,
      transparent 50%
    );
  }
  .card-wrapper {
    .card-info {
      .content {
        .box {
          border-bottom: 1px solid variables.$soft-color;
        }
      }
      .card-image {
        background-color: variables.$card-color;
        .details {
          .flex {
            div {
              h4 {
                color: variables.$text-white;
                font-weight: variables.$fw-400;
              }
            }
          }
        }
      }
    }
  }
  // ... 其余代码按相同规则替换
}

另外确认路径:从文件结构看,_variables.scss和主文件在同一目录,@use "variables";的路径是正确的,无需调整。

内容的提问来源于stack exchange,提问作者Luis Alejandro Lopez de Anda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:55:53