赞
踩
要实现Vue中切换主题颜色的功能,您可以采用以下步骤:
css
:root {
--primary-color: #1890ff; // 默认主题颜色
--secondary-color: #f04864; // 次要颜色
--background-color: #fafafa; // 背景颜色
--text-color: #333; // 字体颜色
}
css
.header {
background-color: var(--primary-color);
color: var(--text-color);
}
data() { return { currentTheme: "default" // 默认主题 }; }, computed: { primaryColor() { const themes = { default: "#1890ff", red: "#f5222d", green: "#52c41a" }; return themes[this.currentTheme] || themes.default; } }, watch: { currentTheme() { document.documentElement.style.setProperty( "--primary-color", this.primaryColor ); } }, methods: { switchTheme(theme) { this.currentTheme = theme; } }
<button @click="switchTheme('default')">Default Theme</button>
<button @click="switchTheme('red')">Red Theme</button>
<button @click="switchTheme('green')">Green Theme</button>
html
<template>
<div class="header">{{title}}</div>
<div class="content">{{content}}</div>
</template>
<style scoped>
.header {
background-color: var(--primary-color);
color: var(--text-color);
}
.content {
background-color: var(--background-color);
}
</style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。