vue中切换主题颜色

一、在styles文件夹里新建一个globalstyle.scss

 

二、在App.vue引入

import './styles/globalstyle.scss'

三、在globalstyle.scss定义变量

 :root {
     --primary-color: #1890ff; // 默认主题颜色
     --background-color: #fafafa; // 背景颜色
     --text-color: #333; // 字体颜色
   }

四、在Vue组件中使用样式变量,使用CSS属性var()来引用

 div { background-color: var( --primary-color);}


五、点击进行主题切换

1、创建一个主题切换方法,使用Vue的计算属性访问样式变量。使用Vue的watch选项监视主题变量的更改。

data() {

 return {
       currentTheme: "default" // 默认主题
     };
   },
   computed: {
     primaryColor() {
       const themes = {
         default: "#1890ff",
         red: "#f5222d",
       };
       return themes[this.currentTheme] || themes.default;
     }
   },
   watch: {
     currentTheme() {
       document.documentElement.style.setProperty(
         "--primary-color",
         this.primaryColor
       );
     }
   },
   methods: {
     switchTheme(theme) {
       this.currentTheme = theme;
     }
   }

2、在Vue中使用切换主题功能。您可以创建一个按钮或下拉菜单来切换主题,并在每个视图组件中使用样式变量。

  <button @click="switchTheme('default')">Default Theme</button>
  <button @click="switchTheme('red')">Red Theme</button>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值