Vue 3 和 Vuetify 3 存取元件中的 SCSS/SASS 變數
P粉974462439
P粉974462439 2023-11-17 13:52:29

我想在我的 Vue 3 元件中使用預定義變數。

<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:未定義的變數。」。 我必須導入什麼才能使用 Vuetify 的全域變數?

P粉974462439
P粉974462439

全部回覆(1)
P粉814160988

這些顏色的使用並不是那麼直接的。

此處記錄了https://vuetifyjs.com/en/功能/sass-variables/#webpack-install 注意需要 sass-loader@^7.0.0 並更改 webpack 設定

如果在元件檔案中匯入顏色變量,您也可以跳過這一點(../ 的數量可能會有所不同)

@import '../../node_modules/vuetify/src/styles/settings/_colors.scss';

接下來要記住的是物件的結構

這裡是摘錄

$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函數來取得基礎紅色

.class{
  background: map-deep-get($colors, "red", "base");
  /* OR */
  background:  map-get($red, "base");
}

所以它看起來像這樣


熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!