vue项目中在scss代码中使用data中的变量

尽管在日常开发中,这类情况实际上很少出现。

VUE2:

在HTML中使用时,请确保将cssVars绑定在需要使用CSS变量的元素或该元素的上层元素上。

html 复制代码
<template>
  <div :style="cssVars">
    <div class="test"/></div>
  </div>
</template>

在data或者compute中给出前缀为--的css变量对象

javascript 复制代码
computed: {
  cssVars() {
    return {
      '--color1': 'red',
      '--color2': 'blue'
    };
  }
}

在css代码中使用

css 复制代码
<style lang="scss" scoped>
.test {
  /deep/ .el-form-item__label {
    color: var(--color) !important;
  }
}
</style>

vue3(v-bind CSS变量注入):

css 复制代码
<template>
  <span> style v-bind CSS变量注入</span>
</template>
<script lang="ts" setup>
  import { ref } from 'vue'
  const color = ref('green')
</script>
<style scoped>
  span {
    /* 使用v-bind绑定组件中定义的变量 */
    color: v-bind('color');
  }
</style>
相关推荐
tedcloud1231 天前
UI-TARS-desktop部署教程:构建AI桌面自动化系统
服务器·前端·人工智能·ui·自动化·github
UXbot1 天前
AI原型设计工具如何支持团队协作与快速迭代
前端·交互·个人开发·ai编程·原型模式
ZC跨境爬虫1 天前
跟着MDN学HTML_day_48:(Node接口)
前端·javascript·ui·html·音视频
PieroPc1 天前
CAMWATCH — 局域网摄像头监控系统 Fastapi + html
前端·python·html·fastapi·监控
巴巴博一1 天前
2026 最新:Trae / Cursor 一键接入 taste-skill 完整教程(让 AI 前端告别“AI 味”)
前端·ai·ai编程
kyriewen1 天前
半夜三点线上崩了,AI替我背了锅——用AI排错,五分钟定位三年老bug
前端·javascript·ai编程
kyriewen1 天前
我让 AI 当了 24 小时全年无休的“毒舌考官”
前端·ci/cd·ai编程
hexu_blog1 天前
vue+java实现图片批量压缩
java·前端·vue.js
IT_陈寒1 天前
为什么你应该学习JavaScript?
前端·人工智能·后端
lifejump1 天前
Empire(帝国)CMS 7.5 XSS注入
前端·安全·xss