1. 在<style>
中使用scss定义的变量和css变量
1. 在@/style/variables.scss文件中定义scss变量
css
// scss变量
$menuText: #bfcbd9;
$menuActiveText: #409eff;
$menuBg: #304156;
// css变量
:root {
--el-menu-active-color: $menuActiveText; // 活动菜单项的文本颜色
--el-menu-background-color: $menuBg; // 菜单的背景颜色
--el-menu-text-color: $menuText; // 菜单的文字颜色
}
2. 在vite.config.ts中引入
- 以前使用的@import已经被废弃了
javascript
/*引入index.scss文件中的变量*/
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "@/style/variables.scss" as *;`,
},
},
},
3.使用
- 暂时发现只能在style中使用,更多使用方法等待慢慢发现
css
<style scoped lang="scss">
.sidebar {
width: 200px;
height: 100vh;
background-color: $menuBg;
}
.el-menu-vertical-demo {
--el-menu-bg-color: var(--el-menu-bg-color);
--el-menu-active-color: var(--el-menu-active-color);
--el-menu-text-color: var(--el-menu-text-color);
}
</style>
2. 在标签和<script>
中使用scss定义的变量
1. 在@/style/variables.module.scss文件中导出常量
javascript
$red: red;
:export {
fontColor: $red;
}
2. 在vue组件中引入
- 可以在组件中传递了
javascript
<script setup lang="ts">
import fc from '@/style/variables.module.scss'
console.log(fc)
</script>
控制台打印结果:
3. 在标签中使用
html
<div :style="{ color: fc.fontColor }">scss变量</div>