news 2026/7/29 3:03:29

vue3+TS 整合element-plus+tailwindcss

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
vue3+TS 整合element-plus+tailwindcss

vue3+TS 整合element-plus和tailwindcss

1. 安装依赖

npminstallelement-plus @element-plus/icons-vue @vueuse/corenpminstall-Dtailwindcss @tailwindcss/vite sass

2. Vite 配置

vite.config.ts

import{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'importtailwindcssfrom'@tailwindcss/vite'exportdefaultdefineConfig({plugins:[vue(),tailwindcss(),],})

3. 全局样式文件

src/style.css

/* 使用 @tailwindcss/vite 方式导入 */@import"tailwindcss";/* 配置暗色模式 */@custom-variantdark(&:where(.dark,.dark *));/* 全局过渡动画 */*{transition:background-color 0.3s ease,color 0.3s ease,border-color 0.3s ease;}/* 滚动条样式 */::-webkit-scrollbar{width:8px;height:8px;}::-webkit-scrollbar-track{background:#f1f1f1;}.dark ::-webkit-scrollbar-track{background:#2a2a2a;}::-webkit-scrollbar-thumb{background:#888;border-radius:4px;}.dark ::-webkit-scrollbar-thumb{background:#555;}::-webkit-scrollbar-thumb:hover{background:#555;}.dark ::-webkit-scrollbar-thumb:hover{background:#666;}

4. 主题 Composable

src/composables/useTheme.ts

import{useDark,useToggle,usePreferredDark}from'@vueuse/core'import{computed,watch,typeRef}from'vue'typeThemeMode='light'|'dark'|'auto'interfaceUseThemeReturn{isDark:Ref<boolean>themeMode:Ref<ThemeMode>toggleTheme:()=>voidsetThemeMode:(mode:ThemeMode)=>void}exportfunctionuseTheme():UseThemeReturn{constpreferredDark=usePreferredDark()constisDark=useDark({storageKey:'theme-mode',valueDark:'dark',valueLight:'light',})constthemeMode=computed<ThemeMode>({get:()=>{constsaved=localStorage.getItem('theme-mode')asThemeMode|nullif(saved==='light'||saved==='dark'){returnsaved}return'auto'},set:(mode:ThemeMode)=>{if(mode==='auto'){localStorage.removeItem('theme-mode')isDark.value=preferredDark.value}else{localStorage.setItem('theme-mode',mode)isDark.value=mode==='dark'}}})consttoggleTheme=useToggle(isDark)constsetThemeMode=(mode:ThemeMode):void=>{themeMode.value=mode}watch(preferredDark,(newVal)=>{if(themeMode.value==='auto'){isDark.value=newVal}})return{isDark,themeMode,toggleTheme,setThemeMode,}}

5. Element Plus 暗色主题配置

src/styles/element-dark.scss

// Element Plus 暗色主题自定义 html.dark { --el-bg-color: #1a1a1a; --el-bg-color-overlay: #1d1d1d; --el-text-color-primary: #e5e5e5; --el-text-color-regular: #cfcfcf; --el-border-color: #4c4d4f; --el-fill-color: #2a2a2a; --el-fill-color-light: #333333; --el-fill-color-lighter: #3a3a3a; --el-fill-color-extra-light: #404040; --el-box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3); }

6. 主题切换组件

src/components/ThemeToggle.vue

<template> <el-switch v-model="isDark" :active-icon="Moon" :inactive-icon="Sunny" @change="toggleTheme" class="theme-switch" /> </template> <script setup lang="ts"> import { computed } from 'vue' import { Moon, Sunny } from '@element-plus/icons-vue' import { useTheme } from '@/composables/useTheme' const { isDark, toggleTheme } = useTheme() </script> <style scoped> .theme-switch { --el-switch-off-color: #409eff; --el-switch-on-color: #303030; } </style>

7. 主题选择器组件(下拉菜单)

src/components/ThemeSelector.vue

<template> <el-dropdown @command="handleCommand"> <div class="flex items-center cursor-pointer p-2 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-lg"> <el-icon :size="20"> <component :is="currentIcon" /> </el-icon> <span class="ml-2 text-sm hidden sm:inline"> {{ currentTheme === 'dark' ? '暗色' : '亮色' }} </span> </div> <template #dropdown> <el-dropdown-menu> <el-dropdown-item command="light"> <el-icon><Sunny /></el-icon> <span class="ml-2">亮色</span> </el-dropdown-item> <el-dropdown-item command="dark"> <el-icon><Moon /></el-icon> <span class="ml-2">暗色</span> </el-dropdown-item> <el-dropdown-item command="auto"> <el-icon><Monitor /></el-icon> <span class="ml-2">跟随系统</span> </el-dropdown-item> </el-dropdown-menu> </template> </el-dropdown> </template> <script setup lang="ts"> import { computed } from 'vue' import { Sunny, Moon, Monitor } from '@element-plus/icons-vue' import { useTheme } from '@/composables/useTheme' const { isDark, themeMode, setThemeMode } = useTheme() const currentIcon = computed(() => { if (themeMode.value === 'auto') return Monitor return isDark.value ? Moon : Sunny }) const currentTheme = computed(() => { if (themeMode.value === 'auto') return 'auto' return isDark.value ? 'dark' : 'light' }) const handleCommand = (command: 'light' | 'dark' | 'auto'): void => { setThemeMode(command) } </script>

8. 主应用文件

src/App.vue

<template> <div class="min-h-screen bg-white dark:bg-gray-900 transition-colors duration-300"> <!-- 顶部导航栏 --> <header class="bg-gray-100 dark:bg-gray-800 shadow-md p-4 transition-colors duration-300"> <div class="container mx-auto flex justify-between items-center"> <h1 class="text-2xl font-bold text-gray-800 dark:text-white"> Vue3 + Element Plus + Tailwind </h1> <div class="flex items-center gap-4"> <ThemeSelector /> <ThemeToggle /> </div> </div> </header> <!-- 主内容 --> <main class="container mx-auto p-6"> <el-card class="mb-4"> <template #header> <span class="text-gray-800 dark:text-white">Element Plus 组件示例</span> </template> <div class="space-y-4"> <el-button type="primary">主要按钮</el-button> <el-button type="success">成功按钮</el-button> <el-button type="warning">警告按钮</el-button> <el-button type="danger">危险按钮</el-button> <el-button type="info">信息按钮</el-button> </div> </el-card> <!-- 使用 Tailwind 的暗色样式 --> <div class="grid grid-cols-1 md:grid-cols-3 gap-4"> <div v-for="i in 3" :key="i" class="p-6 bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 text-gray-800 dark:text-white transition-colors duration-300" > <h3 class="text-lg font-semibold mb-2">卡片 {{ i }}</h3> <p class="text-gray-600 dark:text-gray-300"> 这是一个使用 Tailwind CSS 实现的暗色模式卡片 </p> </div> </div> </main> </div> </template> <script setup lang="ts"> import ThemeToggle from './components/ThemeToggle.vue' import ThemeSelector from './components/ThemeSelector.vue' </script>

9. 主入口文件

src/main.ts

import{createApp}from'vue'importElementPlusfrom'element-plus'import'element-plus/dist/index.css'import'element-plus/theme-chalk/dark/css-vars.css'import'./styles/element-dark.scss'import'./style.css'importAppfrom'./App.vue'constapp=createApp(App)app.use(ElementPlus)app.mount('#app')

10. 最终效果

亮色

暗色

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/29 3:00:46

AI自动化执行新范式:OpenClaw框架解析与应用

1. 扔掉提示词的时代&#xff1a;AI自动化执行新范式2026年的AI领域正在经历一场静默革命——传统的人工编写提示词&#xff08;prompt engineering&#xff09;模式正在被一种更接近生物本能的"饲养"逻辑取代。这种被称为"养龙虾"&#xff08;OpenClaw&am…

作者头像 李华
网站建设 2026/7/29 3:00:20

Kimi K3大模型技术解析:多模态架构与128K长上下文实战指南

1. Kimi K3 技术架构与核心特性解析 Kimi K3 作为月之暗面公司推出的新一代大语言模型&#xff0c;在技术架构上实现了多项突破。从技术视角来看&#xff0c;K3 模型的核心创新主要体现在以下几个方面&#xff1a; 多模态架构设计 &#xff1a;K3 采用了统一的 Transformer 架…

作者头像 李华
网站建设 2026/7/29 2:59:41

AMQP C++ 超详细实战教程(amqp-cpp 开源库从零入门到生产落地)

在后端开发、微服务、任务队列、分布式系统中&#xff0c;RabbitMQ 是最常用的消息中间件&#xff0c;而 RabbitMQ 底层通信标准就是 AMQP 0-9-1&#xff08;高级消息队列协议&#xff09;。C 开发 RabbitMQ 客户端&#xff0c;目前最轻量化、纯C、跨平台、无冗余依赖的开源库就…

作者头像 李华
网站建设 2026/7/29 2:59:29

Bun用Rust重写:性能提升与工程实践全解析

如果你最近关注 JavaScript 工具链的演进&#xff0c;可能会注意到一个有趣的现象&#xff1a;Bun 这个号称要"替代 Node.js"的新星&#xff0c;在宣布用 Rust 重写核心部分后&#xff0c;创造了 16.5 万美元 11 天完成的惊人记录&#xff0c;但随后却陷入了六周未发…

作者头像 李华
网站建设 2026/7/29 2:56:53

四轴无人机DIY组装实战:从飞控接线到Betaflight配置全解析

1. 聚会回顾与核心议题聚焦上周六晚&#xff0c;我们“四轴兴趣小组”的第三次线下聚会圆满结束。这次聚会没有安排宏大的主题演讲&#xff0c;而是回归到最实际、最迫切的问题上——动手组装。现场带来的&#xff0c;不再是PPT和概念图&#xff0c;而是实实在在的PCB板、电机、…

作者头像 李华