vue3:uniapp全局颜色变量配置思路:使用js变量

最开始的思路是创建scss文件,使用$定义变量,像这样:

css 复制代码
$primary-color: #ff5500;
$secondary-color: #52c41a;

然后在uni.scc文件里全局引入

css 复制代码
@import "@/styles/variables.scss";

在vue文件里使用

css 复制代码
<style lang="scss" scoped>
	.title {
		font-size: 36rpx;
		color:$primary-color;
	}
</style>

至此一切顺利,但是!后来我想改变确定对话框里确定按钮的颜色,取primary-color的值。

javascript 复制代码
<script setup>
	const click = ()=> {
		uni.showModal({
			title:'请查看确认按钮为的颜色',
			confirmColor:'#ff5500'
		})
	}
</script>

查阅了很多博客和资料,大多数都是说要创建一个variables.module.scss文件,文件内容如下:

css 复制代码
$primary-color: #ff5500;
$secondary-color: #52c41a;

:export {
  primaryColor: $primary-color;
  secondaryColor: $secondary-color;
}

然后在vite.config.js(uniapp需要手动创建vite.config.js文件)里配置

css 复制代码
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  css: {
    preprocessorOptions: {
      scss: {
		  javascriptEnabled: true,
        // 全局注入 SCSS 变量文件
        additionalData: `@import "./styles/variables.module.scss";`
      }
    }
  }
})

然后在vue文件的style里用$方法获取变量值。

css 复制代码
<style lang="scss" scoped>
	.title {
		font-size: 36rpx;
		color:$primary-color;
	}
</style>

如果要在script里使用,就引入import variables from '../../styles/variables.module.scss'

javascript 复制代码
import variables from '../../styles/variables.module.scss'
const color = variables.primaryColor

但是,这样一顿操作后,开始报重复引入的错误,这也不奇怪,毕竟已经在vite.config.js里引入了,然后又在vue页面里引入了一遍。各种尝试后,我决定放弃这个方案。

放弃全局引入,直接在需要变量的vue文件里引入:

javascript 复制代码
<template>
	<view>
		<text class="title">{{title}}</text>
		<view>{{color}}</view>
		<button @click="click">查看颜色</button>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import variables from '../../styles/variables.module.scss'
	const title = ref('hello uniapp vue3')
	const color = variables.primaryColor
	const click = ()=> {
		uni.showModal({
			title:'请查看确认按钮为的颜色',
			confirmColor:color
		})
	}
</script>

<style lang="scss" scoped>
	.title {
		font-size: 36rpx;
		color: v-bind(color);
	}
</style>

非常完美,是我想要的效果,这个变量值在template、script和style里都能使用。

进一步,我又开始琢磨全局引入的事情,deepseek告诉我,要现在main.js里引入,并且还要配合provide/inject。

javascript 复制代码
import App from './App'
import variables from './styles/variables.module.scss'
import { createSSRApp } from 'vue'

export function createApp() {
  const app = createSSRApp(App)
  // 使用provide提供全局变量
  app.provide('$variables', variables)
  return {
    app
  }
}

vue文件里使用

javascript 复制代码
<script setup>
	import { ref, inject } from 'vue'
	const variables = inject('$variables')
	const title = ref('hello uniapp vue3')
	const color = variables.primaryColor
	const click = ()=> {
		uni.showModal({
			title:'请查看确认按钮为的颜色',
			confirmColor:color
		})
	}
</script>

局部引入只需要一句话,而全局引入完全没让步骤变少!所有果断放弃全局引入。

我思考了还有没有进一步优化空间:

variables.module.scss的本质还是将其转换成js变量,供vue文件使用,并且维护变量的时候,scss变量必须手动去export,容易遗漏。

既然最终是要使用js变量,何不直接在js文件里定义变量?

创建variables.js文件:

javascript 复制代码
const variables = {
	primaryColor:'#ff5500',
	secondaryColor:'#52c41a'
}
export default variables

这样写,变量就方便维护了。

还是在vue文件里局部引入:

javascript 复制代码
<template>
	<view>
		<text class="title">{{title}}</text>
		<view>{{color}}</view>
		<button @click="click">查看颜色</button>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import variables from '../../utils/variables'
	const title = ref('hello uniapp vue3')
	const color = variables.primaryColor
	const click = ()=> {
		uni.showModal({
			title:'请查看确认按钮为的颜色',
			confirmColor:color
		})
	}
</script>

<style lang="scss" scoped>
	.title {
		font-size: 36rpx;
		color: v-bind(color);
	}
</style>

最后的运行效果:

color: v-bind(variables.primaryColor);这样写会报错,把variables.primaryColor存在color变量里,const color = variables.primaryColor 然后color: v-bind(color);

最后的总结:

在js里使用css变量很难,但是在css里使用js变量就容易多了。

相关推荐
开开心心就好5 天前
开源免费高速看图工具,支持漫画大图秒开
linux·运维·服务器·安全·ruby·symfony·1024程序员节
unable code7 天前
磁盘取证-Flying_High
网络安全·ctf·misc·1024程序员节·磁盘取证
unable code8 天前
磁盘取证-ColorfulDisk
网络安全·ctf·misc·1024程序员节·内存取证
unable code9 天前
磁盘取证-[第十章][10.1.2 磁盘取证方法]磁盘取证1
网络安全·ctf·misc·1024程序员节·内存取证
开开心心就好11 天前
免费抽奖工具支持批量导入+自定义主题
linux·运维·服务器·macos·pdf·phpstorm·1024程序员节
开开心心就好15 天前
卸载工具清理残留,检测垃圾颜色标识状态
linux·运维·服务器·python·安全·tornado·1024程序员节
子燕若水16 天前
Facebook reels 运营指南
1024程序员节
尘觉19 天前
创作 1024 天|把热爱写成长期主义
数据库·1024程序员节
写点什么呢20 天前
Word使用记录
word·1024程序员节
开开心心就好20 天前
内存清理工具点击清理,自动间隔自启
linux·运维·服务器·安全·硬件架构·材料工程·1024程序员节