公司移动端项目需要设置多套主题色,后端配置使用哪套移动端显示对应主题色,实现思路为:写对应套数的样式文件,根据接口返回的设置值选用对应样式主题。
1、App文件中进行判断引入主题文件,App.vue:
<script>
export default {
data() {
return {}
},
onLaunch: function() {
console.log('App Launch')
},
onShow: function() {
var themesCss = "default";
uni.setStorageSync('cssStyle', themesCss);
require("./static/themes/" + themesCss + "/css/style.scss");
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style lang="scss">
@import "uview-ui/index.scss";
</style>
2、样式文件中,style.scss:
:root{
--custom-color: #ff6f0d;
--btn-dark:#ff6f0d;
--btn-light:#FFA726;
}
3、使用的文件中使用主题样式:
<style>
.bg{
background-color: var(--custom-color);
}
.bth{
background-image:linear-gradient(45deg, var(--btn-dark), var(--btn-light));
}
</style>
4、因有些框架设置的样式值需在data中定义,无法在css中重写,如uviews框架中tabs的高亮色,此处使用笨办法引用js文件用变量实现,style.js:
export default {
customColor: "#ff6f0d"
}
使用时:
<template>
<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" :active-color="activeColor" font-size="26rpx" inactive-color="#333333"></u-tabs>
</template>
<script>
export default {
data() {
return {
activeColor: '',
css:''
}
},
onLoad() {
this.css = require("@/static/themes/" + uni.getStorageSync('cssStyle') + "/style.js")
this.activeColor = this.css.default.customColor;
}
}
</script>