Element Plus暗黑模式及模式自由切換的實(shí)現(xiàn)
環(huán)境介紹
使用vite構(gòu)建vue項(xiàng)目
1、安裝element plus依賴(lài)
npm install element-plus --save
2、使用element plus組件及導(dǎo)入樣式
// /src/main.js import { createApp } from 'vue' import App from './App.vue' //引入路由器組件 import { router } from './router/index' //引入樣式 import "./styles/index.scss" //引入windicss import 'virtual:windi.css' //引入pinia import { createPinia } from 'pinia'; const pinia = createPinia(); const app = createApp(App) app.use(router) app.use(pinia) //掛在容器 app.mount('#app')
3、element plus 樣式文件
// src/styles/element/light.scss 可以忽略重寫(xiě) 將element的默認(rèn)樣式定義為light /* 重寫(xiě) */ @forward 'element-plus/theme-chalk/src/common/var.scss' with ( $colors: ( 'primary': ( 'base': green, ), ), ); // 導(dǎo)入所有樣式: @use "element-plus/theme-chalk/src/index.scss" as *;
// src/styles/element/drak.scss 暗黑模式 @forward 'element-plus/theme-chalk/src/dark/var.scss' with ( $colors: ( 'primary': ( 'base': green, ), ), ); @use 'element-plus/theme-chalk/src/dark/css-vars.scss' as *;
// src/styles/index.scss 順序不要錯(cuò) 一定要是先默認(rèn)主題 再暗黑主題 這里官網(wǎng)有說(shuō)明 // 參開(kāi)鏈接 https://element-plus.org/zh-CN/guide/dark-mode.html /* 自定義變量? 通過(guò) CSS? 直接覆蓋對(duì)應(yīng)的 css 變量即可 像這樣,新建一個(gè) styles/dark/css-vars.css文件: css html.dark { --el-bg-color: #626aef; } 在 Element Plus 的樣式之后導(dǎo)入它 ts // main.ts */ @use './element/light.scss' as *; @use './element/dark.scss' as *; @use './element/self.scss' as *;
4、安裝pinia 用來(lái)保存切換模式的狀態(tài)
npm install pinia
5、配置pinia
// src/store/theme.js import { defineStore } from 'pinia'; import { watchEffect } from 'vue'; export const useThemeStore = defineStore('theme', { state: () => ({ isDarkTheme: false }), actions: { toggleDarkMode() { this.isDarkTheme =!this.isDarkTheme; }, setupThemeWatcher() { const htmlElement = document.documentElement; watchEffect(() => { htmlElement.classList.toggle('dark', this.isDarkTheme); htmlElement.classList.toggle('light',!this.isDarkTheme); }); } } });
6、創(chuàng)建模式切換的組件
//src/pages/components/ThemeSwitch.vue <template> <el-switch v-model="themeStore.isDarkTheme" active-text="普通模式" inactive-text="暗黑模式" inline-prompt /> </template> <script setup lang='ts'> import { useThemeStore } from '@/store/theme'; const themeStore = useThemeStore(); themeStore.setupThemeWatcher(); </script> <style scoped lang='scss'> </style>
7、使用
<template> <div> <h2>我是登錄頁(yè)面</h2> <theme-switch></theme-switch> </div> </template> <script setup lang='ts'> import DarkModeSwitch from 'components/ThemeSwitch.vue'; </script> <style scoped lang='scss'> </style>
關(guān)于vite的配置
/vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // element plus 相關(guān) import AutoImport from 'unplugin-auto-import/vite' import Components from 'unplugin-vue-components/vite' import { ElementPlusResolver } from 'unplugin-vue-components/resolvers' import WindiCSS from 'vite-plugin-windicss' import path from 'path'; // https://vite.dev/config/ export default defineConfig({ plugins: [ AutoImport({ resolvers: [ElementPlusResolver()], }), Components({ resolvers: [ElementPlusResolver()], }), WindiCSS(), vue() ], css: { preprocessorOptions: { scss: { additionalData: `@use 'src/styles/element/define.scss' as *;` } } }, resolve: { alias: { '@': path.resolve(__dirname, './src'), 'pages': path.resolve(__dirname, './src/pages'), 'components': path.resolve(__dirname, './src/pages/components'), }, }, })
其他配置
//jsconfig.json 如果文件不存在 那么就創(chuàng)建該文件即可 { "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["src/*"], "pages/*": ["src/pages/*"], "components/*": ["src/pages/components/*"] } } }
到此這篇關(guān)于Element Plus暗黑模式及模式自由切換的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Element Plus暗黑模式及模式自由切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?如何綁定disabled屬性讓其不能被點(diǎn)擊
這篇文章主要介紹了vue?如何綁定disabled屬性讓其不能被點(diǎn)擊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04antd vue table跨行合并單元格,并且自定義內(nèi)容實(shí)例
這篇文章主要介紹了antd vue table跨行合并單元格,并且自定義內(nèi)容實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue關(guān)于組件化開(kāi)發(fā)知識(shí)點(diǎn)詳解
在本篇文章里,小編給大家分享的是關(guān)于Vue關(guān)于組件化開(kāi)發(fā)知識(shí)點(diǎn)詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-05-05vue跳轉(zhuǎn)外部鏈接始終有l(wèi)ocalhost的問(wèn)題
這篇文章主要介紹了vue跳轉(zhuǎn)外部鏈接始終有l(wèi)ocalhost的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03詳解vue 動(dòng)態(tài)加載并注冊(cè)組件且通過(guò) render動(dòng)態(tài)創(chuàng)建該組件
這篇文章主要介紹了vue 動(dòng)態(tài)加載并注冊(cè)組件且通過(guò) render動(dòng)態(tài)創(chuàng)建該組件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05手把手教你拿捏vue?cale()計(jì)算函數(shù)使用
這篇文章手把手教你拿捏vue?cale()計(jì)算函數(shù)使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04使用 Vue-TCB 快速在 Vue 應(yīng)用中接入云開(kāi)發(fā)的方法
這篇文章主要介紹了如何使用 Vue-TCB 快速在 Vue 應(yīng)用中接入云開(kāi)發(fā),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02