uniapp 中实现主题色配置切换

简介: uniapp 中实现主题色配置切换

8c8fde6f04386dcdd574ed825b79b70.png

前言

由于最近准备用 uniapp 开发一个新项目 pad 端的 app 项目,并且需要根据不同的子品牌实现一个换肤功能,于是尝试做了一个 uniapp 主题色切换的插件

需求

实现一个通用的theme类,配合<page>页面组件完成整个应用主题色配置,后续项目有类似需求可以直接套用

思路

通过在根组件配置相应主题的类名,以此修改组件内所有样式类的主题色

f2e228928c15c1ca8dff853a508efe5.png

实现

定义主题颜色变量

修改uni.scss

/* 主题色 */
$default-theme: red;
$tx-theme: blue;
$white-text-color: #ffffff;
复制代码

配置主题风格类

新建style文件夹,将类维护在一个 index.scss 文件中

.default-theme {
  .theme-bg {
    background-color: $default-theme;
  }
  .theme-btn {
    background-color: $default-theme;
    color: $white-text-color;
  }
}
.tx-theme {
  .theme-bg {
    background-color: $tx-theme;
    color: $white-text-color;
  }
  .theme-btn {
    background-color: $tx-theme;
    color: $white-text-color;
  }
}
复制代码

引入项目 App.vue

<style lang="scss">
  /*每个页面公共css */
  @import "style/index.scss";
</style>
复制代码

创建页面容器组件

新建 page.vue 组件,作为所有页面的根组件,并在里面实现样式的切换逻辑

<template>
  <view class="default-theme">
    <slot></slot>
  </view>
</template>
复制代码

做一个 demo.vue 页面测试

<template>
  <page>
    <view class="theme-bg">
      <text>测试</text>
    </view>
    <br>
    <button class="theme-btn" @tap="test">请求</button>
  </page>
</template>
复制代码

e73da937bce3551c126f6703a6c072b.png

可以看到我们配置的默认主题样式default-theme在页面中已经生效了,接下来只要切换根组件的样式类名,就可以实现切换效果了

主题切换方法

我们编写一个主题类,里面保存了我们的默认样式,并提供获取样式名,修改样式名的方法

import { ref } from 'vue'
class Theme {
  constructor() {
    this.theme = ref('default-theme')
  }
  get = () => {
    return this.theme.value
  }
  change = (name) => {
    this.theme.value = `${name}-theme`
  }
}
export default Theme
复制代码

全局注册

修改 main.js,将主题方法挂在 uni 对象上

import Theme from '@/lib/theme/index.js'
const theme = new Theme()
uni.$theme = theme
复制代码

根组件样式配置

<template>
  <view :class="theme">
    <slot></slot>
  </view>
</template>
<script>
  export default {
    computed: {
      theme: () => {
        return uni.$theme.get()
      }
    }
  }
</script>
复制代码

demo页面测试切换

methods: {
  change() {
    uni.$theme.change('tx') 
  }
}
复制代码

f601911dbd5a78a7f99fb19f312b0be.png

数据持久化

这样一个主题切换的功能就实现了,但还有个小点,一般主题配置我们需要做本地缓存,我们把主题配置放在 storage

完整代码

  • Theme.js
import { ref } from 'vue'
class Theme {
  constructor() {
    this.theme = ref(null)
    this.init()
  }
  init = () => {
    const theme = uni.getStorageSync('theme') || 'default-theme'
    this.theme.value = theme
    uni.setStorageSync('theme', theme)
  }
  get = () => {
    return this.theme.value
  }
  change = (name) => {
    this.theme.value = `${name}-theme`
    uni.setStorageSync('theme', `${name}-theme`)
  }
}
export default Theme
复制代码
  • Page.vue
<template>
  <view :class="theme">
    <slot></slot>
  </view>
</template>
<script>
  export default {
    computed: {
      theme: () => {
        return uni.$theme.get()
      }
    }
  }
</script>
相关文章
|
8月前
|
小程序 开发者
uniapp合法域名配置
uniapp合法域名配置
206 0
|
10月前
|
小程序
uniapp如何分包 & 分包配置后无法读取static文件夹
uniapp如何分包 & 分包配置后无法读取static文件夹
232 0
uniapp如何分包 & 分包配置后无法读取static文件夹
|
10月前
uniapp manifest.json 完整参数配置参考文档
uniapp manifest.json 完整参数配置参考文档
107 0
|
1月前
uniapp配置tarBar
uniapp配置tarBar
44 0
|
1月前
|
移动开发 监控 小程序
mPaaS常见问题之uniapp ios端云打包的配置config文件如何解决
mPaaS(移动平台即服务,Mobile Platform as a Service)是阿里巴巴集团提供的一套移动开发解决方案,它包含了一系列移动开发、测试、监控和运营的工具和服务。以下是mPaaS常见问题的汇总,旨在帮助开发者和企业用户解决在使用mPaaS产品过程中遇到的各种挑战
|
8月前
|
小程序 API
【 uniapp - 黑马优购 | 首页】小程序首页全局配置(home、网络请求、轮播图、分类...)
【 uniapp - 黑马优购 | 首页】小程序首页全局配置(home、网络请求、轮播图、分类...)
117 0
|
1月前
|
小程序
uniapp 数据配置
uniapp manifest.js 配置
|
9月前
|
小程序 安全 开发工具
uniapp项目实战系列(2):新建项目,项目搭建,微信开发工具的配置
uniapp项目实战系列(2):新建项目,项目搭建,微信开发工具的配置
108 2
|
8月前
|
存储 JavaScript
【 uniapp - 黑马优购 | 加入购物车】如何配置 vuex、加入购物车功能并持久化
【 uniapp - 黑马优购 | 加入购物车】如何配置 vuex、加入购物车功能并持久化
160 0
|
8月前
|
小程序 开发者
【 uniapp - 黑马优购 | tabBar】如何创建和配置标签栏
【 uniapp - 黑马优购 | tabBar】如何创建和配置标签栏
47 0