uni-app x 主题系统完整指南
版本:v1.0
创建日期:2026-01-23
适用框架:uni-app x
📋 目录
💡 提示:详细实现原理请参考 THEME_SYSTEM_IMPLEMENTATION.md
核心思路
类似国际化(i18n)的多套主题系统,根据设备分辨率自动选择适配的主题,使用 Tailwind 风格的命名规范。
核心特点
- 多套主题:5 套主题(mobile-small, mobile-medium, mobile-large, tablet, tablet-large)
- 自动适配:根据屏幕尺寸自动选择最合适的主题
- 变量驱动:所有样式值都从主题变量中读取,统一管理
-
Tailwind 风格:使用熟悉的命名规范(
p-base,rounded-lg,text-base) - 动态切换:支持运行时切换主题(类似 i18n 切换语言)
- 类型安全:使用 UTS 类型定义,保证类型安全
主题映射规则
| 屏幕尺寸 | 主题名称 | 屏幕宽度范围 |
|---|---|---|
| 小屏手机 | mobile-small | < 480px |
| 常规手机 | mobile-medium | 480px - 768px |
| 大屏手机 | mobile-large | 768px - 1024px |
| 平板 | tablet | 1024px - 1920px |
| 大屏平板 | tablet-large | >= 1920px |
方案三:CSS 变量(推荐)⭐
为什么推荐 CSS 变量?
-
✅ 官方推荐:uni-app x 官方文档明确支持var()函数 -
✅ 性能最好:CSS 变量由 CSS 引擎处理,只需在根元素设置一次 -
✅ 代码量少:只需定义一次 CSS,值由逻辑层动态注入 -
✅ 灵活性高:可以任意组合使用,支持所有 CSS 属性 -
✅ 易于维护:变量名固定,值统一管理
工作原理
1. 应用启动
↓
2. 检测屏幕尺寸 → 自动选择主题(如 mobile-medium)
↓
3. 从主题配置读取变量值(如 spacing.base = 16px)
↓
4. 在根元素设置 CSS 变量(--p-base: 16px)
↓
5. 所有子元素通过两种方式使用:
- 方式1:直接使用预定义类名(class="p-base")⭐ 推荐
- 方式2:在自定义 CSS 中使用变量(padding: var(--p-base))
为什么有两种使用方式?
theme/theme-variables.css 已经定义了所有常用类名,比如:
.p-base { padding: var(--p-base); }.rounded-lg { border-radius: var(--r-lg); }.text-base { font-size: var(--t-base); }
所以:
-
方式 1(推荐):直接使用类名
class="p-base",无需自定义 CSS - 方式 2:在自定义 CSS 中使用变量,用于组合多个变量或添加额外样式
实现逻辑详解
1. 主题选择与变量生成
// utils/theme-manager.uts
// 1. 检测屏幕尺寸,自动选择主题
const themeName = getThemeByScreenSize() // 如:'mobile-medium'
// 2. 从主题配置读取变量值
const theme = themeManager.getCurrentTheme()
// theme.spacing.base = 16px (mobile-medium)
// theme.borderRadius.lg = 8px (mobile-medium)
2. CSS 变量设置(在根元素)
<template>
<view :style="themeVars">
<!-- 子元素 -->
</view>
</template>
<script setup lang="uts">
import { useThemeVariables } from '@/composables/useThemeVariables.uts'
const { themeVars } = useThemeVariables()
// themeVars 包含:
// {
// '--p-base': '16px',
// '--p-lg': '20px',
// '--r-lg': '8px',
// '--t-base': '14px',
// ...
// }
</script>
3. 预定义类名(在 theme-variables.css)
/* theme/theme-variables.css */
/* 这些类名已经定义好,直接使用即可 */
.p-base {
padding: var(--p-base); /* 使用 CSS 变量,值由根元素设置 */
}
.rounded-lg {
border-radius: var(--r-lg);
}
.text-base {
font-size: var(--t-base);
}
.px-lg {
padding-left: var(--p-lg);
padding-right: var(--p-lg);
}
.py-md {
padding-top: var(--p-md);
padding-bottom: var(--p-md);
}
4. 使用方式对比
方式 1:直接使用类名(推荐)
<template>
<!-- 直接使用预定义类名,无需自定义 CSS -->
<view class="p-base rounded-lg">
<text class="text-base">内容</text>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
/* 无需额外 CSS,类名已定义好 */
</style>
工作原理:
- 根元素设置了
--p-base: 16px -
.p-base类使用var(--p-base) - 浏览器解析为
padding: 16px
方式 2:在自定义 CSS 中使用变量
<template>
<view class="custom-card">
<text class="custom-text">内容</text>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
/* 自定义样式,组合使用多个变量 */
.custom-card {
padding: var(--p-base); /* 使用变量 */
border-radius: var(--r-lg); /* 使用变量 */
background-color: #fff; /* 固定值 */
margin-bottom: var(--p-md); /* 使用变量 */
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); /* 固定值 */
}
.custom-text {
font-size: var(--t-base);
color: #333;
}
</style>
工作原理:
- 根元素设置了
--p-base: 16px - 自定义 CSS 中使用
var(--p-base) - 浏览器解析为
padding: 16px
两种方式都支持,推荐使用方式 1(更简洁)
5. 完整工作流程
应用启动
↓
useThemeVariables() 初始化
↓
检测屏幕尺寸(uni.getWindowInfo())
↓
自动选择主题(getThemeByScreenSize())
↓
加载主题配置(loadThemeConfig())
↓
生成 CSS 变量值(getThemeVariablesStyle())
↓
绑定到根元素 :style(themeVars)
↓
所有子元素通过两种方式使用:
- 方式1:class="p-base"(使用预定义类名)
- 方式2:padding: var(--p-base)(在自定义 CSS 中使用变量)
快速开始
步骤 1:在 App.uvue 中初始化
<template>
<!-- 在根元素上绑定 CSS 变量 -->
<view :style="themeVars">
<!-- 应用内容会通过 pages.json 自动加载 -->
</view>
</template>
<script setup lang="uts">
import { useThemeVariables } from '@/composables/useThemeVariables.uts'
const { themeVars } = useThemeVariables()
// themeVars 包含:{ '--p-base': '16px', '--r-lg': '8px', ... }
</script>
<style>
/* 引入主题 CSS 变量定义(包含所有预定义类名) */
@import '@/theme/theme-variables.css';
</style>
步骤 2:在页面中使用
theme/theme-variables.css 已经定义了所有常用类名,支持两种使用方式:
方式 1:直接使用预定义类名(推荐)
<template>
<view class="p-page">
<view class="p-base rounded-lg bg-white">
<text class="text-base">响应式文本</text>
</view>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
/* 只需添加颜色等自定义样式 */
.bg-white {
background-color: #fff;
}
</style>
方式 2:在自定义 CSS 中使用变量(用于组合样式)
当需要组合多个变量或自定义复杂样式时:
<template>
<view class="custom-card">
<text class="custom-title">响应式文本</text>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
/* 自定义样式,组合使用多个变量 */
.custom-card {
padding: var(--p-base);
border-radius: var(--r-lg);
background-color: #fff;
margin-bottom: var(--p-md);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.custom-title {
font-size: var(--t-lg);
margin-bottom: var(--p-sm);
color: #333;
}
</style>
推荐:优先使用方式 1(预定义类名),更简洁高效。方式 2 用于需要组合多个变量或添加额外样式的场景。
完整示例
示例 1:基础使用(推荐:直接使用类名)
<template>
<view class="p-page">
<view class="p-base rounded-lg bg-white mb-md">
<text class="text-lg mb-sm text-dark">标题</text>
<text class="text-base text-gray">描述文本</text>
</view>
</view>
</template>
<script setup lang="uts">
// 无需任何 JavaScript 代码!
// CSS 变量会自动根据当前主题设置值
</script>
<style>
@import '@/theme/theme-variables.css';
/* 只需添加颜色等自定义样式 */
.bg-white {
background-color: #fff;
}
.mb-md {
margin-bottom: var(--p-md);
}
.mb-sm {
margin-bottom: var(--p-sm);
}
.text-dark {
color: #333;
}
.text-gray {
color: #666;
}
</style>
说明:
-
p-page、p-base、rounded-lg、text-lg等都是预定义的类名(在theme-variables.css中已定义) - 直接使用类名,无需在 CSS 中重复定义
- 只需添加颜色、背景等自定义样式
- 如果
theme-variables.css中没有的类名(如mb-md、mt-sm等),可以在自定义 CSS 中使用变量
示例 2:卡片列表(推荐:直接使用类名)
<template>
<view class="p-page">
<view
v-for="item in list"
:key="item.id"
class="p-base rounded-lg bg-white mb-md"
>
<text class="text-lg mb-sm text-dark">{{ item.title }}</text>
<text class="text-base text-gray">{{ item.desc }}</text>
</view>
</view>
</template>
<script setup lang="uts">
const list = ref([
{ id: 1, title: '标题1', desc: '描述1' },
{ id: 2, title: '标题2', desc: '描述2' },
{ id: 3, title: '标题3', desc: '描述3' }
])
</script>
<style>
@import '@/theme/theme-variables.css';
/* 只需添加颜色等自定义样式 */
.bg-white {
background-color: #fff;
}
.mb-md {
margin-bottom: var(--p-md);
}
.mb-sm {
margin-bottom: var(--p-sm);
}
.text-dark {
color: #333;
}
.text-gray {
color: #666;
}
</style>
说明:
- 直接使用预定义类名(
p-base、rounded-lg、text-lg等),代码更简洁 -
theme-variables.css中已定义:.p-*、.px-*、.py-*、.m-*、.rounded-*、.text-*、.w-*、.h-* - 如果需要的类名没有预定义(如
mb-md、mt-sm等),可以在自定义 CSS 中使用变量
示例 3:按钮组件(推荐:直接使用类名)
<template>
<button class="btn rounded-base" :class="sizeClass">
<text class="text-base">{{ text }}</text>
</button>
</template>
<script setup lang="uts">
const props = defineProps<{
text: string
size?: 'sm' | 'base' | 'lg'
}>()
const sizeClass = computed(() => {
const size = props.size ?? 'base'
// 直接使用预定义的类名组合
if (size === 'sm') {
return 'py-sm px-md'
} else if (size === 'lg') {
return 'py-lg px-xl'
} else {
return 'py-md px-lg'
}
})
</script>
<style>
@import '@/theme/theme-variables.css';
.btn {
background-color: #007aff;
color: #fff;
border: none;
}
</style>
说明:使用预定义的 py-*、px-* 类名,无需自定义 CSS。
CSS 变量命名规范
| 变量前缀 | 含义 | 示例 | 说明 |
|---|---|---|---|
--p-* |
间距(padding/margin) |
--p-base, --p-lg
|
用于 padding、margin |
--r-* |
圆角(radius) |
--r-lg, --r-full
|
用于 border-radius |
--t-* |
字体大小(text) |
--t-base, --t-lg
|
用于 font-size |
--s-* |
尺寸(size) |
--s-base, --s-lg
|
用于 width、height |
可用变量和类名
间距变量和类名(--p-* / .p-*)
方式 1:直接使用类名(推荐)
<view class="p-base"> <!-- padding: var(--p-base) -->
<view class="px-lg"> <!-- padding-left/right: var(--p-lg) -->
<view class="py-md"> <!-- padding-top/bottom: var(--p-md) -->
方式 2:在 CSS 中使用变量
.custom {
padding: var(--p-base);
padding-left: var(--p-lg);
padding-top: var(--p-md);
}
可用变量:
-
var(--p-0)/.p-0- 0px -
var(--p-xs)/.p-xs- 4-8px(根据主题) -
var(--p-sm)/.p-sm- 6-12px -
var(--p-md)/.p-md- 10-20px -
var(--p-base)/.p-base- 12-24px -
var(--p-lg)/.p-lg- 16-32px -
var(--p-xl)/.p-xl- 20-40px -
var(--p-2xl)/.p-2xl- 24-48px -
var(--p-3xl)/.p-3xl- 32-64px -
var(--p-4xl)/.p-4xl- 40-80px -
var(--p-page)/.p-page- 页面边距(等于 --p-base)
方向类名:
-
.px-xs,.px-sm,.px-md,.px-base,.px-lg,.px-xl- 左右内边距 -
.py-xs,.py-sm,.py-md,.py-base,.py-lg,.py-xl- 上下内边距
圆角变量和类名(--r-* / .rounded-*)
方式 1:直接使用类名(推荐)
<view class="rounded-lg"> <!-- border-radius: var(--r-lg) -->
<view class="rounded-full"> <!-- border-radius: 9999px -->
方式 2:在 CSS 中使用变量
.custom {
border-radius: var(--r-lg);
}
可用变量:
-
var(--r-none)/.rounded-none- 0px -
var(--r-sm)/.rounded-sm- 2-6px -
var(--r-base)/.rounded-base- 4-8px -
var(--r-md)/.rounded-md- 6-10px -
var(--r-lg)/.rounded-lg- 8-14px -
var(--r-xl)/.rounded-xl- 10-18px -
var(--r-2xl)/.rounded-2xl- 12-24px -
var(--r-full)/.rounded-full- 9999px(圆形)
字体大小变量和类名(--t-* / .text-*)
方式 1:直接使用类名(推荐)
<text class="text-base">内容</text> <!-- font-size: var(--t-base) -->
<text class="text-lg">标题</text> <!-- font-size: var(--t-lg) -->
方式 2:在 CSS 中使用变量
.custom {
font-size: var(--t-base);
}
可用变量:
-
var(--t-xs)/.text-xs- 10-14px -
var(--t-sm)/.text-sm- 12-16px -
var(--t-base)/.text-base- 14-18px -
var(--t-md)/.text-md- 16-20px -
var(--t-lg)/.text-lg- 18-24px -
var(--t-xl)/.text-xl- 20-28px -
var(--t-2xl)/.text-2xl- 24-32px -
var(--t-3xl)/.text-3xl- 28-40px -
var(--t-4xl)/.text-4xl- 32-48px
尺寸变量和类名(--s-* / .w-*, .h-*)
方式 1:直接使用类名(推荐)
<view class="w-base h-lg"> <!-- width: var(--s-base), height: var(--s-lg) -->
方式 2:在 CSS 中使用变量
.custom {
width: var(--s-base);
height: var(--s-lg);
}
可用变量:
-
var(--s-xs)/.w-xs,.h-xs- 16-28px -
var(--s-sm)/.w-sm,.h-sm- 24-36px -
var(--s-md)/.w-md,.h-md- 32-44px -
var(--s-base)/.w-base,.h-base- 40-52px -
var(--s-lg)/.w-lg,.h-lg- 48-64px -
var(--s-xl)/.w-xl,.h-xl- 64-80px
手动切换主题
<script setup lang="uts">
import { useTheme } from '@/composables/useTheme.uts'
import { useThemeVariables } from '@/composables/useThemeVariables.uts'
const { setTheme } = useTheme()
const { updateThemeVariables } = useThemeVariables()
// 切换到平板主题
function switchToTablet() {
setTheme('tablet')
updateThemeVariables() // 更新 CSS 变量
}
</script>
性能优势
-
✅ 只需设置一次:在根元素设置 CSS 变量,所有子元素自动继承 -
✅ CSS 引擎优化:由原生 CSS 引擎处理,性能最优 -
✅ 无 JavaScript 开销:无需运行时计算样式值 -
✅ 内存占用小:CSS 变量共享,不占用额外内存
性能对比(200 个元素):
- 方案一(动态样式):约 20-50ms
- 方案二(主题化类名):约 5-10ms
-
方案三(CSS 变量):< 1ms
⭐
方案二:主题化类名
适用场景
- 团队熟悉 Tailwind 风格
- 需要大量工具类
- 不介意 CSS 文件体积较大
快速使用
<template>
<view :class="tw('p-base', 'rounded-lg', 'flex', 'flex-col')">
<text :class="tw('text-base')">响应式文本</text>
</view>
</template>
<script setup lang="uts">
import { tw } from '@/utils/theme-classes.uts'
// tw() 会自动添加当前主题前缀
// 返回: 'theme-mobile-medium p-base rounded-lg flex flex-col'
</script>
<style>
@import '@/common-theme.css';
</style>
工作原理
- 为每个主题生成一套 CSS 类(如
.theme-mobile-medium .p-base) - 使用
tw()函数根据当前主题添加前缀 - 浏览器匹配对应的 CSS 类并应用样式
优缺点
优点:
-
✅ 性能好(CSS 类) -
✅ Tailwind 风格命名 -
✅ 易于调试(可在 DevTools 查看类名)
缺点:
-
⚠ ️ CSS 文件体积大(约 50-100KB) -
⚠ ️ 需要为每个主题生成完整 CSS -
⚠ ️ 维护成本较高
方案一:动态样式绑定
适用场景
- 少量动态样式元素(< 50 个)
- 需要高度自定义
- 原型开发阶段
快速使用
<template>
<view :style="cardStyle">
<text :style="textStyle">响应式文本</text>
</view>
</template>
<script setup lang="uts">
import { computed } from 'vue'
import { useTheme } from '@/composables/useTheme.uts'
const { spacing, borderRadius, fontSize } = useTheme()
const cardStyle = computed(() => ({
padding: spacing.value('base') + 'px',
borderRadius: borderRadius.value('lg') + 'px'
}))
const textStyle = computed(() => ({
fontSize: fontSize.value('base') + 'px'
}))
</script>
工作原理
- 在 JavaScript 中根据当前主题计算样式值
- 通过
:style绑定到元素 - Vue 将样式对象转换为内联样式
优缺点
优点:
-
✅ 灵活性高 -
✅ 代码简洁 -
✅ 易于理解
缺点:
-
⚠ ️ 性能开销(每次渲染计算) -
⚠ ️ 大量元素时影响明显 -
⚠ ️ 无法利用 CSS 引擎优化
架构设计
系统架构
┌─────────────────────────────────────────┐
│ 主题配置文件 (themes.json) │
│ - mobile-small (小屏手机) │
│ - mobile-medium (常规手机) │
│ - mobile-large (大屏手机) │
│ - tablet (平板) │
│ - tablet-large (大屏平板) │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ 主题管理器 (theme-manager.uts) │
│ - 根据屏幕尺寸自动选择主题 │
│ - 提供主题切换接口 │
│ - 提供主题变量获取接口 │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ 主题变量生成器 (theme-variables.uts) │
│ - 根据当前主题生成 CSS 变量值 │
│ - 返回样式对象供 :style 绑定 │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ CSS 变量 (var(--p-base)) │
│ - 在根元素设置,所有子元素继承 │
│ - 性能最优,代码量最少 │
└─────────────────────────────────────────┘
核心逻辑
1. 主题选择逻辑
// utils/theme-manager.uts
export function getThemeByScreenSize(): ThemeName {
const screenSize = getScreenSize()
switch (screenSize) {
case 'small': return 'mobile-small'
case 'medium': return 'mobile-medium'
case 'large': return 'mobile-large'
case 'xlarge':
// 根据屏幕宽度进一步判断
const width = uni.getWindowInfo().windowWidth
return width >= 1920 ? 'tablet-large' : 'tablet'
default: return 'mobile-medium'
}
}
2. CSS 变量生成逻辑
// utils/theme-variables.uts
export function getThemeVariablesStyle(): Record<string, string> {
const themeManager = getThemeManager()
const cssVars: Record<string, string> = {}
// 生成间距变量
cssVars['--p-base'] = themeManager.getSpacing('base') + 'px'
cssVars['--p-lg'] = themeManager.getSpacing('lg') + 'px'
// ... 其他变量
// 生成圆角变量
cssVars['--r-lg'] = themeManager.getBorderRadius('lg') + 'px'
// ... 其他变量
// 生成字体大小变量
cssVars['--t-base'] = themeManager.getFontSize('base') + 'px'
// ... 其他变量
return cssVars
}
3. 自动适配流程
应用启动
↓
useThemeVariables() 初始化
↓
检测屏幕尺寸(uni.getWindowInfo())
↓
自动选择主题(getThemeByScreenSize())
↓
加载主题配置(loadThemeConfig())
↓
生成 CSS 变量值(getThemeVariablesStyle())
↓
绑定到根元素 :style
↓
所有子元素通过 var() 使用
文件结构
项目根目录/
├── themes/
│ └── themes.json # 主题配置文件(JSON)
│
├── theme/
│ └── theme-variables.css # CSS 变量定义(方案三)
│
├── utils/
│ ├── theme-manager.uts # 主题管理器(核心逻辑)
│ ├── theme-variables.uts # CSS 变量生成器(方案三)
│ ├── theme-classes.uts # 主题化类名工具(方案二)
│ └── screen-adapt.uts # 屏幕适配工具
│
├── composables/
│ ├── useTheme.uts # 主题 Composable(基础)
│ └── useThemeVariables.uts # CSS 变量 Composable(方案三)
│
├── common-theme.css # 主题化类名 CSS(方案二)
│
└── docs/
├── THEME_SYSTEM.md # 本文档(汇总)
├── THEME_SYSTEM_ARCHITECTURE.md # 架构设计文档
├── THEME_SYSTEM_USAGE.md # 使用指南
├── THEME_SYSTEM_COMPARISON.md # 方案对比
└── THEME_CSS_VARIABLES_EXAMPLE.md # CSS 变量示例
最佳实践
1. 推荐使用方案三(CSS 变量)
理由:
- 性能最好
- 代码量最少
- 官方推荐
- 易于维护
2. 混合使用策略
<template>
<!-- 根元素:设置 CSS 变量 -->
<view :style="themeVars">
<!-- 布局:使用静态类名 -->
<view class="flex flex-col items-center">
<!-- 主题样式:直接使用预定义类名(推荐) -->
<view class="p-base rounded-lg">
<text class="text-base">内容</text>
</view>
<!-- 或者:在自定义 CSS 中使用变量 -->
<view class="custom-card">
<text class="custom-text">内容</text>
</view>
</view>
</view>
</template>
<style>
@import '@/theme/theme-variables.css';
/* 自定义样式,组合使用多个变量 */
.custom-card {
padding: var(--p-base);
border-radius: var(--r-lg);
background-color: #fff;
}
.custom-text {
font-size: var(--t-base);
color: #333;
}
</style>
组合规则:
-
预定义类名:优先使用(
class="p-base rounded-lg"),最简洁 -
CSS 变量:用于自定义样式组合(
padding: var(--p-base)) -
静态类名:用于布局相关的样式(
flex、items-center等) - 动态样式:仅用于特殊情况(高度自定义)
3. 性能优化建议
-
缓存计算结果:使用
computed缓存样式对象 - 避免频繁切换:主题切换后统一更新,不要逐个元素更新
- 合理使用变量:只对需要主题化的样式使用变量,固定值直接用 CSS
4. 代码组织建议
<!-- 页面结构 -->
<template>
<view :style="themeVars" class="page">
<!-- 页面内容 -->
</view>
</template>
<script setup lang="uts">
// 1. 导入主题相关
import { useThemeVariables } from '@/composables/useThemeVariables.uts'
// 2. 初始化主题变量
const { themeVars } = useThemeVariables()
// 3. 其他业务逻辑
// ...
</script>
<style>
/* 1. 引入主题 CSS 变量 */
@import '@/theme/theme-variables.css';
/* 2. 页面特定样式 */
.page {
display: flex;
flex-direction: column;
}
</style>
5. 调试技巧
-
查看当前主题:
const { currentThemeName } = useTheme() console.log('当前主题:', currentThemeName.value) -
查看 CSS 变量值:
const { themeVars } = useThemeVariables() console.log('CSS 变量:', themeVars.value) -
在 DevTools 中查看:
- 打开浏览器 DevTools
- 查看根元素的
style属性 - 查看子元素使用的
var()变量
常见问题
Q1: CSS 变量在子页面不生效?
A: 确保在页面的根元素上也设置了 CSS 变量:
<template>
<view :style="themeVars">
<!-- 页面内容 -->
</view>
</template>
Q2: 如何自定义主题变量?
A: 修改 themes/themes.json 文件,或直接修改 utils/theme-manager.uts 中的 defaultThemeConfig。
Q3: 可以同时使用多个方案吗?
A: 可以,但建议统一使用方案三(CSS 变量),避免混乱。
Q4: 性能影响大吗?
A: 方案三(CSS 变量)性能影响最小,几乎可以忽略。方案一(动态样式)在大量元素时会有明显影响。
总结
推荐方案
首选:方案三(CSS 变量)
- 性能最好
- 代码量最少
- 官方推荐
- 易于维护
备选:方案二(主题化类名)
- 适合 Tailwind 风格爱好者
- 性能好但文件大
临时:方案一(动态样式)
- 仅用于少量元素或特殊场景
核心优势
-
✅ 自动适配:根据屏幕尺寸自动选择主题 -
✅ 统一管理:所有样式值集中管理 -
✅ 类型安全:UTS 类型定义,编译时检查 -
✅ 易于扩展:添加新主题或新变量很简单 -
✅ 性能优秀:CSS 变量方案性能最优
实现原理详解
为什么有两种使用方式?
theme/theme-variables.css 已经定义了所有常用类名,比如:
.p-base { padding: var(--p-base); }.rounded-lg { border-radius: var(--r-lg); }.text-base { font-size: var(--t-base); }
所以:
-
方式 1(推荐):直接使用类名
class="p-base",无需自定义 CSS - 方式 2:在自定义 CSS 中使用变量,用于组合多个变量或添加额外样式
完整实现流程
1. 应用启动
↓
2. useThemeVariables() 初始化
↓
3. 检测屏幕尺寸(uni.getWindowInfo())
↓
4. 自动选择主题(getThemeByScreenSize())
↓
5. 加载主题配置(loadThemeConfig())
↓
6. 生成 CSS 变量值(getThemeVariablesStyle())
↓
7. 绑定到根元素 :style(themeVars)
↓
8. 所有子元素通过两种方式使用:
- 方式1:class="p-base"(使用预定义类名)⭐ 推荐
- 方式2:padding: var(--p-base)(在自定义 CSS 中使用变量)
预定义类名列表
已预定义的类名(可直接使用):
- 间距:
.p-*,.px-*,.py-*,.m-*,.p-page - 圆角:
.rounded-* - 字体:
.text-* - 尺寸:
.w-*,.h-*
未预定义的类名(需自定义):
- Margin 方向:
.mb-*,.mt-*,.ml-*,.mr-* - 组合类:
.gap-*,.space-*
详细实现原理请参考:THEME_SYSTEM_IMPLEMENTATION.md
文档版本:v1.0
最后更新:2026-01-23
维护者:uni-app x 重构团队