首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >组件中的Vue 3和Vue 3 access SCSS/SASS Varriable

组件中的Vue 3和Vue 3 access SCSS/SASS Varriable
EN

Stack Overflow用户
提问于 2022-08-13 00:51:05
回答 1查看 442关注 0票数 0

我想在Vue 3组件中使用预定义的变量。

代码语言:javascript
复制
<template>
  <div class="rounded img-container">
    <span v-if="!props.ready">Du musst noch ein Bild aufnehmen!</span>
  </div>
</template>

<script lang="ts" setup>
import { defineProps } from "vue";

const props = defineProps({
  ready: { type: Boolean, required: true }
})
</script>
<style lang="scss" scoped>

.img-container {
  aspect-ratio: 3 / 2;
  margin-left: auto;
  margin-right: auto;
  background: $red; <- Error
}

.text-small {
  font-size: 2em;
}
</style>

不幸的是,我得到了一个错误"SassError: un定义变量。“我需要导入什么才能使用Vuetify的全局变量?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-13 04:14:35

这些颜色的使用并不是,即直接使用

这里记录了https://vuetifyjs.com/en/features/sass-variables/#webpack-install注释Requires sass-loader@^7.0.0和webpack配置的更改。

如果在组件文件中导入颜色变量( ../的数量可能有所不同),也可以跳过这一步。

代码语言:javascript
复制
@import '../../node_modules/vuetify/src/styles/settings/_colors.scss';

接下来要记住的是对象的结构。

这是一段节选

代码语言:javascript
复制
$red: () !default;
$red: map-deep-merge(
  (
    'base': #F44336,
    'lighten-5': #FFEBEE,
    'lighten-4': #FFCDD2,
    'lighten-3': #EF9A9A,
    'lighten-2': #E57373,
    'lighten-1': #EF5350,
    'darken-1': #E53935,
    'darken-2': #D32F2F,
    'darken-3': #C62828,
    'darken-4': #B71C1C,
    'accent-1': #FF8A80,
    'accent-2': #FF5252,
    'accent-3': #FF1744,
    'accent-4': #D50000
  ),
  $red
);

/* other colors... */

$colors: () !default;
$colors: map-deep-merge(
  (
    'red': $red,
    'pink': $pink,
    'purple': $purple,
    'deep-purple': $deep-purple,
    'indigo': $indigo,
    'blue': $blue,
    'light-blue': $light-blue,
    'cyan': $cyan,
    'teal': $teal,
    'green': $green,
    'light-green': $light-green,
    'lime': $lime,
    'yellow': $yellow,
    'amber': $amber,
    'orange': $orange,
    'deep-orange': $deep-orange,
    'brown': $brown,
    'blue-grey': $blue-grey,
    'grey': $grey,
    'shades': $shades
  ),
  $colors
);

因此,颜色不是映射到字符串,而是映射到对象(请参阅map-deep-merge),因此不能使用$red为您提供颜色值。

相反,您可以使用map-deep-get函数获取基红色。

代码语言:javascript
复制
.class{
  background: map-deep-get($colors, "red", "base");
  /* OR */
  background:  map-get($red, "base");
}

所以看起来会是这样

代码语言:javascript
复制
<style lang="scss" scoped>

@import '../../node_modules/vuetify/src/styles/settings/_colors.scss';
.img-container {
  aspect-ratio: 3 / 2;
  margin-left: auto;
  margin-right: auto;
  background:  map-deep-get($colors, "red", "base");
  /* OR */
  background:  map-get($red, "base");
}

.text-small {
  font-size: 2em;
}
</style>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73340896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档