当前位置:首页 > VUE

vue主题色实现

2026-01-19 07:00:31VUE

实现 Vue 主题色的方法

使用 CSS 变量动态切换主题色

src/assets/styles 目录下创建 theme.css 文件,定义主题色变量:

:root {
  --primary-color: #409EFF;
  --secondary-color: #67C23A;
}
.dark-theme {
  --primary-color: #304156;
  --secondary-color: #4F88C6;
}

main.js 中导入样式文件:

import '@/assets/styles/theme.css'

组件中使用 CSS 变量:

<template>
  <button :style="{ backgroundColor: 'var(--primary-color)' }">按钮</button>
</template>

切换主题的 JavaScript 代码:

function toggleTheme() {
  document.documentElement.classList.toggle('dark-theme')
}

使用 Vuex 管理主题状态

安装必要依赖:

npm install vuex

创建 store/modules/theme.js

const state = {
  currentTheme: 'light'
}

const mutations = {
  SET_THEME(state, theme) {
    state.currentTheme = theme
  }
}

export default {
  namespaced: true,
  state,
  mutations
}

在组件中切换主题:

vue主题色实现

methods: {
  changeTheme() {
    const newTheme = this.$store.state.theme.currentTheme === 'light' ? 'dark' : 'light'
    this.$store.commit('theme/SET_THEME', newTheme)
    document.documentElement.className = newTheme + '-theme'
  }
}

使用 SCSS 变量和混合

创建 src/assets/styles/variables.scss

$themes: (
  light: (
    primary-color: #409EFF,
    secondary-color: #67C23A
  ),
  dark: (
    primary-color: #304156,
    secondary-color: #4F88C6
  )
);

创建 src/assets/styles/mixins.scss

@mixin theme-aware($key, $color) {
  @each $theme-name, $theme-color in $themes {
    .#{$theme-name}-theme & {
      #{$key}: map-get($theme-color, $color)
    }
  }
}

组件中使用:

.button {
  @include theme-aware('background-color', 'primary-color');
}

使用第三方库

安装 vue-theme-switcher

vue主题色实现

npm install vue-theme-switcher

基本使用:

import Vue from 'vue'
import ThemeSwitcher from 'vue-theme-switcher'

Vue.use(ThemeSwitcher, {
  themes: {
    light: {
      primary: '#409EFF'
    },
    dark: {
      primary: '#304156'
    }
  }
})

在组件中切换:

this.$theme.set('dark')

响应式主题切换实现

创建主题监听器:

const prefersDark = window.matchMedia('(prefers-color-scheme: dark)')
prefersDark.addListener(e => {
  const theme = e.matches ? 'dark' : 'light'
  this.$store.commit('theme/SET_THEME', theme)
})

持久化主题选择:

// 保存到localStorage
localStorage.setItem('user-theme', theme)

// 初始化时读取
const savedTheme = localStorage.getItem('user-theme') || 'light'
this.$store.commit('theme/SET_THEME', savedTheme)

标签: 主题vue
分享给朋友:

相关文章

vue实现图片轮播

vue实现图片轮播

使用 Swiper 实现图片轮播 安装 Swiper 依赖 npm install swiper 在 Vue 组件中引入 Swiper <template> <div…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

实现vue模版

实现vue模版

Vue 模板的基本结构 Vue 模板基于 HTML 语法,通过 Vue 的指令和插值实现动态渲染。以下是一个基础模板示例: <template> <div id="…

vue el 实现

vue el 实现

Vue 中使用 Element UI (el) 的实现方法 Element UI 是一个基于 Vue 的组件库,提供丰富的 UI 组件,常用于快速构建企业级中后台产品。以下介绍 Vue 项目中集成和使…

vue实现看板

vue实现看板

Vue 实现看板功能 使用 Vue 实现看板功能可以通过组件化开发、状态管理和拖拽库结合完成。以下是一个完整的实现方案: 基础项目结构 src/ ├── components/ │ ├── Ka…