【使用 Vue2 脚手架创建项目并实现主题切换功能涵盖Ant-Design-Vue2/Element-UI】

使用 Vue2 脚手架创建项目并实现主题切换功能

初始化 Vue2 项目
复制代码
vue create theme-switch-demo
cd theme-switch-demo
安装依赖(根据选择的 UI 库)

对于 Ant Design Vue 2:

复制代码
npm install ant-design-vue@2.x

对于 Element UI:

复制代码
npm install element-ui

方案一:使用 Ant Design Vue 2 实现主题切换

创建主题样式文件

src/assets 下创建两个主题文件:

  • ant-dark-theme.less
less 复制代码
@primary-color: #1890ff;
@layout-header-background: #001529;
@body-background: #000;
@component-background: #141414;
@text-color: rgba(255, 255, 255, 0.85);
  • ant-light-theme.less
less 复制代码
@primary-color: #1890ff;
@layout-header-background: #fff;
@body-background: #f0f2f5;
@component-background: #fff;
@text-color: rgba(0, 0, 0, 0.85);
配置 vue.config.js
javascript 复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          javascriptEnabled: true,
        }
      }
    }
  }
})
主题切换组件
vue 复制代码
<template>
  <a-config-provider :theme="currentTheme">
    <div :class="['app-container', theme]">
      <a-button @click="toggleTheme">切换主题</a-button>
      <!-- 其他内容 -->
    </div>
  </a-config-provider>
</template>

<script>
import { theme } from 'ant-design-vue'
const { darkAlgorithm, defaultAlgorithm } = theme

export default {
  data() {
    return {
      theme: 'light',
    }
  },
  computed: {
    currentTheme() {
      return {
        algorithm: this.theme === 'dark' ? darkAlgorithm : defaultAlgorithm,
      }
    }
  },
  methods: {
    toggleTheme() {
      this.theme = this.theme === 'light' ? 'dark' : 'light'
      document.documentElement.setAttribute('data-theme', this.theme)
    }
  }
}
</script>

<style lang="less">
.app-container {
  height: 100vh;
  padding: 20px;
  transition: all 0.3s;

  &.dark {
    background: #000;
    color: rgba(255, 255, 255, 0.85);
  }

  &.light {
    background: #f0f2f5;
    color: rgba(0, 0, 0, 0.85);
  }
}
</style>

方案二:使用 Element UI 实现主题切换

安装主题工具
复制代码
npm install element-theme element-theme-chalk -D
创建主题配置文件

在项目根目录创建 element-variables.scss:

scss 复制代码
/* 深色主题 */
$--color-primary: #409EFF;
$--background-color-base: #222;
$--color-text-regular: #eee;
$--border-color-base: #444;

/* 浅色主题 */
// $--color-primary: #409EFF;
// $--background-color-base: #f5f7fa;
// $--color-text-regular: #606266;
// $--border-color-base: #dcdfe6;
构建主题
复制代码
node_modules/.bin/et -i element-variables.scss
node_modules/.bin/et
主题切换组件
vue 复制代码
<template>
  <div :class="['app-container', theme]">
    <el-button @click="toggleTheme">切换主题</el-button>
    <!-- 其他内容 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      theme: 'light'
    }
  },
  methods: {
    toggleTheme() {
      this.theme = this.theme === 'light' ? 'dark' : 'light'
      this.loadTheme()
    },
    loadTheme() {
      const theme = this.theme
      const oldLink = document.getElementById('theme-style')
      
      if (oldLink) {
        oldLink.remove()
      }
      
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.type = 'text/css'
      link.id = 'theme-style'
      link.href = `./theme/${theme}/index.css`
      
      document.head.appendChild(link)
    }
  },
  mounted() {
    this.loadTheme()
  }
}
</script>

<style>
.app-container {
  height: 100vh;
  padding: 20px;
  transition: all 0.3s;
}

.app-container.dark {
  background: #222;
  color: #eee;
}

.app-container.light {
  background: #f5f7fa;
  color: #606266;
}
</style>
构建主题文件

将生成的 ./theme 文件夹复制到 public 目录下,确保生产环境能访问到主题文件。

全局状态管理(可选)

对于大型应用,建议使用 Vuex 管理主题状态:

安装 Vuex
复制代码
npm install vuex
创建 store
javascript 复制代码
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    theme: 'light'
  },
  mutations: {
    toggleTheme(state) {
      state.theme = state.theme === 'light' ? 'dark' : 'light'
    }
  }
})

然后在组件中使用:

javascript 复制代码
computed: {
  theme() {
    return this.$store.state.theme
  }
},
methods: {
  toggleTheme() {
    this.$store.commit('toggleTheme')
  }
}

以上两种方案分别实现了 Ant Design Vue 2 和 Element UI 的主题切换功能,可根据项目需求选择合适的 UI 库方案。

相关推荐
NCDS程序员9 小时前
v-model: /v-model/ :(v-bind)三者核心区别
前端·javascript·vue.js
小杨同学呀呀呀呀10 小时前
Ant Design Vue <a-timeline>时间轴组件失效解决方案
前端·javascript·vue.js·typescript·anti-design-vue
Mr Xu_18 小时前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠18 小时前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript
未来之窗软件服务20 小时前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
phltxy21 小时前
Vue 核心特性实战指南:指令、样式绑定、计算属性与侦听器
前端·javascript·vue.js
Byron07071 天前
Vue 中使用 Tiptap 富文本编辑器的完整指南
前端·javascript·vue.js
Byron07071 天前
从 0 到 1 搭建 Vue 前端工程化体系:提效、提质、降本实战落地
前端·javascript·vue.js
zhengfei6111 天前
【AI平台】- 基于大模型的知识库与知识图谱智能体开发平台
vue.js·语言模型·langchain·知识图谱·多分类
徐小夕@趣谈前端1 天前
Web文档的“Office时刻“:jitword共建版2.0发布!让浏览器变成本地生产力
前端·数据结构·vue.js·算法·开源·编辑器·es6