基于mixin混入和css3的uniapp主题切换

定义导出不同主题的theme.js 主题配置

//全局样式管理
export default {
	defaultTheme: {
		"tabbar": {
			'backgroundColor': '#fff',
			"color": "#999",
			"selectedColor": "#f42",
		},
		"globalStyle": {
			"navigationBarBackgroundColor":"#F8F8F8",
			"navigationBarTextStyle":"black"
		},
		"loading": {
			"loadingPath": "/static/loading/default/loading.gif",
			"loadingBg": 'rgba(255,255,255,0.8)',
			"loadingWidth": 40,
			"loadingHeight": 40
		},
		"tabbarIconList": [{
				"iconPath": "static/icon/default/home.png",
				"selectedIconPath": "static/icon/default/homeSelect.png",
			},
			{
				"iconPath": "static/icon/default/me.png",
				"selectedIconPath": "static/icon/default/meSelect.png",
			}
		],
		"style": {
			"--text-color": "#f42",
			"--background-color": "#fff",
		}
	},
	newTheme: {
		'tabbar': {
			'backgroundColor': '#000',
			"color": "#999",
			"selectedColor": "#fff",
		},
		"globalStyle": {
			"navigationBarBackgroundColor":"#000",
			"navigationBarTextStyle":"white"
		},
		"loading": {
			"loadingPath": "/static/loading/new/loading.gif",
			"loadingBg": 'rgba(0,0,0,0.8)',
			"loadingWidth": 70,
			"loadingHeight": 30
		},
		"tabbarIconList": [{
				"iconPath": "static/icon/new/home.png",
				"selectedIconPath": "static/icon/new/homeSelect.png",
			},
			{
				"iconPath": "static/icon/new/me.png",
				"selectedIconPath": "static/icon/new/meSelect.png",
			}
		],
		"style": {
			"--text-color": "#fff",
			"--background-color": "#000",
		}
	}
}

封装themeMixin.js 全局使用

import themeInfo from "./theme.js"
import {
	themeStore
} from "../store/theme.js"

/**
 * 全局混入型
 */
export default {
	data() {
		return {
			theme: {},
			themeName: 'default',
			tabbar: [{
					"text": "首页",
					"pagePath": "pages/index/index"
				},
				{
					"text": "个人中心",
					"pagePath": "pages/personalCenter/personalCenter"
				}
			]
		}
	},
	methods: {
		setTheme(themeName, isrest = true) {
			console.log(themeName, themeInfo)
			const store = themeStore()
			//根据主题设置 tabbar 默认和选中颜色
			uni.setTabBarStyle({
				...themeInfo[themeName + 'Theme'].tabbar
			})
			setTimeout(() => {
				//根据主题设置 导航栏颜色
				uni.setNavigationBarColor({
					frontColor: themeInfo[themeName + 'Theme'].globalStyle.navigationBarTextStyle,
					backgroundColor: themeInfo[themeName + 'Theme'].globalStyle
						.navigationBarBackgroundColor
				})
			}, 100)
			//根据主题设置 tabbar 颜色和icon
			this.tabbar.forEach((item, index) => {
				uni.setTabBarItem({
					index: index,
					...themeInfo[themeName + 'Theme'].tabbarIconList[index],
					...item
				})
			})
			this.themeName = themeName
			this.theme = themeInfo[themeName + 'Theme'].style
			//缓存loading 配置
			uni.setStorageSync('loading', themeInfo[themeName + 'Theme'].loading)
			//缓存 颜色配置和当前主题名
			store.SET_THEME({
				theme: this.theme,
				themeName
			})
			if (isrest) {
				plus.runtime.restart()
			}
		}
	},
	onLoad() {
		let themeName = uni.getStorageSync('themeName')
		this.setTheme(themeName || 'default', false)
	}
}

main.js 引入themeMixin

import {
	createSSRApp
} from 'vue'
import App from './App.vue'
import themeMixin from "./mixin/themeMixin.js"
import {
	createPinia
} from 'pinia'
export function createApp() {
	const app = createSSRApp(App)
	app.mixin(themeMixin)
	app.use(createPinia())
	return {
		app
	}
}

封装pinia存取主题

import {
	defineStore
} from 'pinia'

export const themeStore = defineStore('theme', {
	state: () => {
		return {
			theme: {},
			themeName:''
		}
	},
	getters: {},
	actions: {
		SET_THEME({
			theme,
			themeName
		}) {
			this.theme = theme ? theme : {}; // for null or undefined value
			this.themeName = themeName
			// setAuthCache(TOKEN_KEY, info);
			uni.setStorageSync('themeName', themeName)
			uni.setStorageSync('theme', theme)
		},
		GET_THEME(){
			return this.theme?this.theme:uni.setStorageSync('theme', theme)
		}
	}
})

页面中使用

<template>
   //读取全局theme配置
	<view class="page" :style="theme">
		<button >地图</button>
	</view>
</template>

<script setup>
	//js中使用
	import {
		themeStore
	} from "../../store/theme.js"
	const {
		GET_THEME
	} = themeStore()
	const styleTheme = GET_THEME()
	console.log(styleTheme["--theme-color"])
</script>

<style lang="scss">
	.page{
		padding-top: var(--status-bar-height);
		background: var(--background-color);
	}
	button {
		color: var(--text-color);
		background-color: var(--background-color);
		border-radius: 0;
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狗_都不做前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值