当前位置: 首页 > news >正文

原生微信小程序如何动态配置主题颜色及如何调用子组件的方法

一、最终效果

在这里插入图片描述

二、步骤

1、在初始化进入项目时,获取当前主题色
2、把主题色定义成全局变量(即在app.js中设置)
3、tabBar也需要定义全局变量,在首页时需要重新赋值

三、具体实现

1、app.js

onLaunch () {//获取主题数据this.setTabBarData()
}// 设置主题色async setTabBarData() {// 后台接口返回的主题色配置const res = await request('/appWechatFitment/getStyleContact', 'GET')let themeType = res.data ? res.data.themeStyle : 'green_theme'switch (themeType) {case 'green_theme':this.globalData.bgColor = '#e3f3ef' // page 背景色this.globalData.modalBgColor = '#edfff2' // 模块背景色this.globalData.color = '#4ca464' // 字体按钮主色this.globalData.blurColor = 'rgba(75, 117, 86, 0.74)' // 未被选中的元素颜色this.globalData.colorName = 'green'// 全局css变量----需要在每个wxml页面的最顶层view标签动态配置stylethis.globalData.themeColor = '--themeBgColor: #e3f3ef;--themeColor: #4ca464;--themeBlurColor: rgba(75, 117, 86, 0.74);--themeModalColor: #edfff2;'break;case 'red_theme':this.globalData.bgColor = '#f7f8fa'this.globalData.modalBgColor = 'rgba(230, 66, 66, 0.08)'this.globalData.color = '#e64242'this.globalData.blurColor = '#c0b9b9'this.globalData.colorName = 'red'this.globalData.themeColor = '--themeBgColor: #f7f8fa;--themeColor: #e64242;--themeBlurColor: #c0b9b9;--themeModalColor: rgba(230, 66, 66, 0.08);'break;}// 导航栏配置let tabBarData = { "background_color": "#FFFFFF", "inactive_color": '#7DA288', "active_color": this.globalData.color, "data": [{ "text": "商城", "page": "setup", "pagePath": "pages/home/home", "iconPath": "assets/imgs/tabbar/home-" + themeType + ".png", "selectedIconPath": "assets/imgs/tabbar/home-" + themeType + "-active.png" }, { "pagePath": "pages/category/index", "iconPath": "assets/imgs/tabbar/category-" + themeType + ".png", "selectedIconPath": "assets/imgs/tabbar/category-" + themeType + "-active.png", "text": "分类" }, { "pagePath": "pages/delivery/index", "iconPath": "assets/imgs/tabbar/invoicing-" + themeType + ".png", "selectedIconPath": "assets/imgs/tabbar/invoicing-" + themeType + "-active.png", "text": "叫水", }, { "pagePath": "pages/usercenter/index", "iconPath": "assets/imgs/tabbar/me-" + themeType + ".png", "selectedIconPath": "assets/imgs/tabbar/me-" + themeType + "-active.png", "text": "我的" }] }this.globalData.tabBarData = tabBarData// 导航栏赋值wx.setNavigationBarColor({backgroundColor: this.globalData.color,frontColor: '#ffffff'})}

2、首页

const app = getApp()
onShow() {
this.setTabBar()
}
setTabBar() {const tabBarData = app.globalData.tabBarDatalet isTabBarSet = app.globalData.isTabBarSet// 设置page样式wx.setPageStyle({style: {background: app.globalData.bgColor,color: '#101010'}})// 导航栏赋值wx.setNavigationBarColor({backgroundColor: app.globalData.color,frontColor: '#ffffff'})// 是否已经对标签栏赋值if (app.globalData.tabBarData && !isTabBarSet) {app.globalData.isTabBarSet = truewx.setTabBarStyle({color: tabBarData.inactive_color,selectedColor: tabBarData.active_color,backgroundColor: '#fff',borderStyle: 'white'})tabBarData.data.forEach((item, index) => {wx.setTabBarItem({index: index,text: item.text,pagePath: item.pagePath,iconPath: item.iconPath,selectedIconPath: item.selectedIconPath})})}}

3、常规页面设置

1、js

const app = getApp()
Page({data: {themeColor: app.globalData.themeColor}
onShow() {wx.setNavigationBarColor({backgroundColor: app.globalData.color,frontColor: '#ffffff'})}wx.setPageStyle({style: {background: app.globalData.bgColor,color: '#101010'}})
})

2、wxml文件

最顶级view标签

<view style="{{themeColor}}">
.....
</view>

四、完成以上步骤,那么wxss文件就可以使用css变量来使用

var(–themeBgColor)---- --themeBgColor就是wxml文件顶级style样式

.t-button {--td-button-default-color: #000;--td-button-primary-text-color: var(--themeBgColor);
}

五、如何调用子组件的方法

1、有子组件<deliveryBox\/>且此组件有个getCount方法

2、那么在父组件中,只需要在使用的子组件中加上一个ID

<deliveryBox id="deliveryBox" />

3、父组件js中需要加上

this.selectComponent("#deliveryBox").getCount()

相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

http://www.lryc.cn/news/270127.html

相关文章:

  • Java关键字(1)
  • 【机器学习合集】深度生成模型 ->(个人学习记录笔记)
  • Java将PDF转换为文本
  • Linux 运维工具之1Panel
  • 深入了解小红书笔记详情API:为内容创新提供动力
  • Animate 2024(Adobe an2024)
  • 尽量避免删改List
  • 【Linux操作系统】探秘Linux奥秘:用户、组、密码及权限管理的解密与实战
  • 计算机组成原理复习4
  • AutoSAR(基础入门篇)3.3-Autosar中RTE的数据一致性与Interface接口
  • 超维空间S2无人机使用说明书——52、初级版——使用PID算法进行基于yolo的目标跟踪
  • <JavaEE> TCP 的通信机制(一) -- 确认应答 和 超时重传
  • Spark任务调度与数据本地性
  • 【论文阅读】Self-Paced Curriculum Learning
  • C++简易线程池
  • 【MATLAB】PSO粒子群优化LSTM(PSO_LSTM)的时间序列预测
  • 产品经理学习-怎么写PRD文档
  • 第3课 获取并播放音频流
  • Spark编程实验四:Spark Streaming编程
  • Flink去重计数统计用户数
  • 力扣:62. 不同路径(动态规划,附python二维数组的定义)
  • 2022年全球运维大会(GOPS深圳站)-核心PPT资料下载
  • 8868体育助力意甲罗马俱乐部 迪巴拉有望付出
  • java设计模式实战【策略模式+观察者模式+命令模式+组合模式,混合模式在支付系统中的应用】
  • 小程序wx:if 和hidden的区别?
  • 自动驾驶学习笔记(二十三)——车辆控制模型
  • Linux Shell 015-文本双向覆盖重定向工具tee
  • 【PyQt】(自定义类)QIcon派生,更易用的纯色Icon
  • 【mysql】数据处理格式化、转换、判断
  • 深入探索Java中的UDP网络通信机制