Element-UI Plus 暗黑主题切换及自定义主题色

1. 暗黑主题切换

  1. 在main.js中引入下面文件
js 复制代码
import 'element-plus/theme-chalk/dark/css-vars.css'
  1. 安装 @vueuse/core
bash 复制代码
pnpm add @vueuse/core
  1. App.vue 添加下面代码

使用了 useDark() 的页面才会从 localStorage中读取当前主题状态,否则,刷新页面就会恢复默认主题

在App.vue 添加 useDark() 所有页面都可以复用

js 复制代码
import {useDark} from '@vueuse/core'
useDark()
  1. 案例1: 按钮 控制暗黑模式切换
html 复制代码
<script setup>
import {useDark, useToggle} from "@vueuse/core";

const isDark = useDark()
const toggleDark = useToggle(isDark)
</script>

<template>
	<el-button @click="toggleDark()">{{ isDark ? '简白主题' : '暗黑主题' }}</el-button>
</template>
  1. 案例2: switch 控制暗黑模式切换
bash 复制代码
<script setup>
import {useDark} from "@vueuse/core"
const isDark=useDark()
</script>

<template>
	<el-switch v-model="isDark"></el-switch>
</template>

2. 自定义主题色

  1. 安装插件
bash 复制代码
pnpm add use-element-plus-theme
  1. 修改主题色
js 复制代码
import {useElementPlusTheme} from 'use-element-plus-theme'
const color='#efefef'
localStorage.setItem('themeColor',color) // 持久化主题色
useElementPlusTheme(color) // 应用主题色
  1. 全局应用主题色 (App.vue中添加下面代码)
bash 复制代码
import {useElementPlusTheme} from "use-element-plus-theme";
useElementPlusTheme(localStorage.getItem('themeColor'))
相关推荐
速易达网络25 分钟前
Vue3 原生移动应用开发来了
前端·javascript·css
渣哥28 分钟前
别再乱用了!Spring AOP 与 AspectJ 的区别比你想的复杂
javascript·后端·面试
患得患失94935 分钟前
【Turborepo】【Next】 Turborepo + Next.js 前后端精简搭建笔记(带官网)
开发语言·javascript·笔记
拓端研究室35 分钟前
视频讲解|Python遗传算法GA在车辆路径规划VRP数据优化中的应用
开发语言·人工智能·r语言
小谭鸡米花1 小时前
高德地图电子围栏/地图选区/地图打点
前端·javascript·vue.js
xxxxxxllllllshi1 小时前
Cookie、Session、JWT、SSO,网站与 APP 登录持久化与缓存
java·开发语言·jvm·数据结构·缓存·面试
西瓜树枝1 小时前
解决 JS 大整数精度丢失?一文读懂 BigInt 的底层逻辑与实战规则
前端·javascript
刺客_Andy1 小时前
React 第四十六节 Router中useInRouterContext的使用详细介绍及注意事项
前端·javascript·react.js
刺客_Andy1 小时前
React 第四十四节Router中 usefetcher的使用详解及注意事项
前端·javascript·react.js
AI+程序员在路上1 小时前
QT6中QToolBox功能与应用
开发语言·qt