【问题标题】:How to extend a variable's map in SCSS?如何在 SCSS 中扩展变量的映射?
【发布时间】:2020-09-18 13:41:30
【问题描述】:

我正在使用一个名为 Buefy 的包,它是 Bulma CSS 框架库的 Vue.js 包装器。 Buefy 在其模板组件上放置了一个名为 type 的属性/属性(例如type="is-warning")。根据Buefy documentation,术语“is-warning”是根据 SCSS 中设置的$colors 变量预定义的:

所以我按照these instructions from Buefy 来自定义$colors 映射,但是我想做的是将这些指令中定义的初始SCSS 保留在单个base.scss 文件中,然后使用我自己定制的 SCSS 文件进行扩展。

我已将base.scss 文件包含在我的Vue 项目中,并在vue.config.js 文件中包含以下sn-p:

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        prependData: `
          @import "@/assets/scss/base.scss";
        `
      }
    }
  }
}

所以我的问题是,如何使用自己的名称和值扩展 base.scss 中的 $colors 映射?

这是来自 base.scss 的 sn-p,其中定义了 $colors 映射:

```css
$colors: (
  "white": ($white, $black),
  "black": ($black, $white),
  "light": ($light, $light-invert),
  "dark": ($dark, $dark-invert),
  "primary": ($primary, $primary-invert),
  "info": ($info, $info-invert),
  "success": ($success, $success-invert),
  "warning": ($warning, $warning-invert),
  "danger": ($danger, $danger-invert),
  "twitter": ($twitter, $twitter-invert)
);
```

所以,我的问题再次是如何在base.scss(可能在App.vue)之外的不同文件中扩展$colors 映射以保持原始不变?我没有'在scss docs 中看不到任何解决方案。

【问题讨论】:

  • @kris 我不明白这如何解释如何扩展 $colors 映射或允许我所描述的替代模式。 Vue.js 的.vue 文件有一个<styles lang="scss"> 部分,scss 代码会自动编译。

标签: css vue.js sass bulma buefy


【解决方案1】:

您的自定义颜色值将放入一个名为 $custom-colors 的变量(后来合并到 $colors)中,它实际上在 "derived variables" 上的文档中有所描述。

并对其进行自定义:

// Import Bulma's core
@import "~bulma/sass/utilities/_all";

// Set your colors
$custom-colors: (
  "facebook": ($blue, $white),
  "linkedin": ($dark-blue, $white)
  // etc.
);

// Setup $colors to use as bulma classes (e.g. 'is-twitter')
$colors: (
  "white": ($white, $black),
  "black": ($black, $white),
  "light": ($light, $light-invert),
  "dark": ($dark, $dark-invert),
  "primary": ($primary, $primary-invert),
  "info": ($info, $info-invert),
  "success": ($success, $success-invert),
  "warning": ($warning, $warning-invert),
  "danger": ($danger, $danger-invert),
  "twitter": ($twitter, $twitter-invert)
);

// Import Bulma and Buefy styles
@import "~bulma";
@import "~buefy/src/scss/buefy";

它在内部使用名为mergeColorMaps 的自定义函数。

【讨论】:

  • 感谢您的回答。然而,这需要我在base.scss 中定义我的$custom-colors。有没有办法让base.scss 不受影响?理想情况下,我想在App.vue 中定义$custom-colors(如果您熟悉Vue.js)。但如果这不可能,那么我想我需要导入到 base.css 以获取 myStyles.scss 我在其中创建自定义定义的位置?
  • @Raj 您不一定需要在那里重新定义变量。如果您按照上面显示的imports 的顺序(以及在文档中),您应该能够在其他地方定义这个$custom-colors 并在布尔玛样式之前导入它。
  • 所以基本上,可以将myStyles.scss 正确之前 base.scss 导入到App.vue 或您的入口文件。
  • 我刚刚尝试设置$custom-colors,但除非我将变量命名为$colors,否则它永远不会起作用。是因为 Buefy 使用的是 0.7.5 版本的 Bulma 吗?
  • @Raj 很奇怪。它在derived-variables.sass 中有明确定义。您是否能够创建一个可重现的示例(jsfiddle 或 codeandbox)来展示它如何无法为您呈现正确的结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多