当前位置:首页 > 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
}

在组件中切换主题:

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

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实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式…

vue实现访问量

vue实现访问量

Vue 实现访问量统计的方法 使用前端计数 在 Vue 中可以通过简单的计数器实现页面访问量的统计。这种方法适合小型项目或演示用途。 // 在 Vue 组件中 data() { return…

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &…

vue无限菜单怎么实现

vue无限菜单怎么实现

实现 Vue 无限菜单的方法 递归组件实现嵌套菜单 使用 Vue 的递归组件特性可以轻松实现无限级嵌套菜单。通过组件调用自身的方式处理嵌套数据。 <template> <ul&…

vue实现导航栏切图

vue实现导航栏切图

Vue 实现导航栏切图 使用 Vue Router 实现基础导航 在 Vue 项目中,可以通过 Vue Router 实现导航栏的路由切换。创建一个导航栏组件,并在其中使用 <router-li…

vue中登陆实现逻辑

vue中登陆实现逻辑

Vue 中登录实现逻辑 前端实现逻辑 表单验证 使用 Vue 的表单验证库(如 VeeValidate)或自定义验证规则,确保用户输入的账号和密码符合要求。例如,检查邮箱格式、密码长度等。 发送登…