当前位置:首页 > 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实现选区创建

Vue 实现选区创建的方法 在 Vue 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ran…

vue实现导航栏切图

vue实现导航栏切图

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

vue中登陆实现逻辑

vue中登陆实现逻辑

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

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双…

vue实现计时

vue实现计时

Vue 实现计时功能 在 Vue 中实现计时功能可以通过多种方式完成,以下是几种常见的方法: 使用 setInterval 实现基础计时器 通过 setInterval 和 clearInterv…