【vue3主题切换】

vue3主题切换

目录结构

bash 复制代码
src/
├── store/
│   └── theme.ts            # Pinia 管理主题
├── styles/
│   └── theme.scss          # CSS变量样式
├── App.vue
├── main.ts

CSS 变量配置(src/styles/theme.scss)

复制代码
:root,
[data-theme='light'] {
    --el-bg-color: #ffffff;
    --el-text-color-primary: #303133;
    --layout-bg-color: #f3f3f4;
}

[data-theme='dark'] {
    --el-bg-color: #1e1e1e;
    --el-text-color-primary: #e5eaf3;
    --layout-bg-color: #121212;
}

Pinia 主题 store(src/store/theme.ts)

ts 复制代码
import { defineStore } from 'pinia'

export const useThemeStore = defineStore('theme', {
    state: () => ({
        mode: 'light' as 'light' | 'dark',
        primary: '#1C84C6'
    }),
    actions: {
        loadTheme() {
            // 从 localStorage 获取保存的模式,如果没有则使用默认值 'light'
            // 使用类型断言确保值只能是 'light' 或 'dark'
            const m = localStorage.getItem('mode') as 'light' | 'dark' || 'light'
            // 从 localStorage 获取保存的主色调,如果没有则使用默认值 '#1C84C6'
            const p = localStorage.getItem('primary') || '#1C84C6'

            // 更新存储库的状态
            this.mode = m
            this.primary = p
            document.documentElement.setAttribute('data-theme', m)
            document.documentElement.style.setProperty('--el-color-primary', p)
        },
        setMode(mode: 'light' | 'dark') {
            this.mode = mode
            document.documentElement.setAttribute('data-theme', mode)
            localStorage.setItem('mode', mode)
        },
        setPrimary(color: string) {
            this.primary = color
            document.documentElement.style.setProperty('--el-color-primary', color)
            localStorage.setItem('primary', color)
        }
    }
})

入口文件(src/main.ts)

ts 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import 'element-plus/dist/index.css'
import pinia from '@/store'
import '@/styles/theme.scss'

const app = createApp(App)
app.use(pinia)

import { useThemeStore } from './store/theme'
const ts = useThemeStore()
ts.loadTheme()

app.mount('#app')

主题抽屉组件

ts 复制代码
<!-- 主题抽屉 -->
    <el-drawer v-model="drawer" title="主题设置" size="20%">
      <el-form>
        <el-form-item label="主题颜色">
          <el-color-picker @change="onColorChange" v-model="color" show-alpha
        /></el-form-item>
        <el-form-item label="暗黑模式">
          <el-switch v-model="isDark" @change="onModeChange"
        /></el-form-item>
      </el-form>
    </el-drawer>

import { useThemeStore } from "@/store/modules/theme";
const themeStore = useThemeStore();

const color = computed({
  get: () => themeStore.primary,
  set: (val: string) => themeStore.setPrimary(val),
});

// 暗黑模式绑定
const isDark = computed({
  get: () => themeStore.mode === "dark",
  set: (val: boolean) => {
    const mode = val ? "dark" : "light";
    themeStore.setMode(mode);
  },
});

// 可选,也可以直接在 computed set 内处理
function onColorChange(color: string) {
  themeStore.setPrimary(color);
}
function onModeChange(val: boolean) {
  themeStore.setMode(val ? "dark" : "light");
}

全局样式引用变量

ts 复制代码
<style scoped>
.page {
  background-color: var(--layout-bg-color);
  color: var(--el-text-color-primary);
}
</style>
相关推荐
harrain4 天前
vue2开发环境搭建指南
vue
by__csdn4 天前
Electron+Vite:实现electron + vue3 + ts + pinia + vite高效跨平台开发指南
前端·javascript·vue.js·typescript·electron·node.js·vue
人工智能训练5 天前
前端框架选型破局指南:Vue、React、Next.js 从差异到落地全解析
运维·javascript·人工智能·前端框架·vue·react·next.js
李纲明6 天前
WordPress外贸成品网站的免费获取渠道
vue·php
加洛斯6 天前
前端小知识002:ref 与 reactive 详解
前端·vue
计算机毕设vx_bysj68697 天前
计算机毕业设计必看必学~Springboot教学进度管理系统,原创定制程序、单片机、java、PHP、Python、小程序、文案全套、毕设成品等!
java·spring boot·vue·课程设计·管理系统
小贺要学前端8 天前
【无标题】
前端·javascript·vue·技术趋势
IT教程资源C8 天前
(N_141)基于springboot,vue网上拍卖平台
mysql·vue·前后端分离·拍卖系统·springboot拍卖
IT教程资源C8 天前
(N_144)基于微信小程序在线订餐系统
mysql·vue·uniapp·前后端分离·订餐小程序·springboot订餐
合作小小程序员小小店9 天前
web网页开发,在线短视频管理系统,基于Idea,html,css,jQuery,java,springboot,mysql。
java·前端·spring boot·mysql·vue·intellij-idea