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

一、最终效果

在这里插入图片描述

二、步骤

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标签动态配置style
        this.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.tabBarData
       let 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 = true
         wx.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.niftyadmin.cn/n/5296502.html

相关文章

php接口优化 使用curl_multi_init批量请求

PHP使用CURL同时抓取多个URL地址 抓取多个URL地址是Web开发中常见的需求&#xff0c;使用PHP的curl库可以简化这个过程。本文将详细介绍如何使用PHP的curl库同时请求多个URL地址&#xff0c;并提供具体的代码案例和注释。 curl库介绍 curl是一个常用的开源网络传输工具&…

Tensorflow2.X的GPU版框架最快最稳搭建方法

一、环境基础 Windows10以上 已装Anaconda 支持GPU 二、搭建步骤 1. 在Anaconda中创建并进入虚拟环境 conda create -n envname python3.8 conda activate envname 注意&#xff1a;envname 替换为你自己想命名的&#xff0c;下文将以“Ljdenv”出现 2.安…

WPF实现拖拽获取文件

使用场景&#xff1a;直接拖动视频到软件中就可以实现视频的播放 步骤一&#xff1a;在xaml的Window容器中添加&#xff1a; AllowDrop"True" Drop"Window_Drop" -- 这个时候会在code_behind中注册事件Window_Drop 步骤二&#xff1a; 然后转到注册的事…

计算机组成原理-总线概述

文章目录 总线简图总线的物理实现总览总线定义总线的特性总线的分类按数据格式分类串行总线并行总线 按总线功能分类注意系统总线的进一步分类 总线的结构单总线的机构双总线的结构三总线的结构四总线的结构 小结 总线简图 总线的物理实现 如果该为数据总线&#xff0c;那么当…

【损失函数】SmoothL1Loss 平滑L1损失函数

1、介绍 torch.nn.SmoothL1Loss 是 PyTorch 中的一个损失函数&#xff0c;通常用于回归问题。它是 L1 损失和 L2 损失的结合&#xff0c;旨在减少对异常值的敏感性。 loss_function nn.SmoothL1Loss(reductionmean, beta1.0) 2、参数 size_average (已弃用): 以前用于确定是…

python2.x文件声明解释器编码方法

1 python2.x文件声明解释器编码方法 python2.x解释器默认使用ASCII编码方法编码和解码。 通过sys**.getdefaultencoding()**获得python解释器默认编码方法。 编写python文件时&#xff0c;在文件第一行或第二行&#xff0c;声明python2.x解释器使用的编码方法&#xff0c;比…

【hcie-cloud】【15】华为云Stack应急预案

文章目录 前言应急预案介绍应急预案诉求用户挑战业务上云阻碍大应急保障缺乏规范应急时效性难以保障运维体系存在盲点 应急预案的必要性应急预案故障场景总览基础云服务应用平台数据库大数据平台AI服务 应急预案故障场景介绍故障场景案例 (1)故障场景案例 (2) IaaS层应急预案计…

Hadoop安装笔记1单机/伪分布式配置_Hadoop3.1.3——备赛笔记——2024全国职业院校技能大赛“大数据应用开发”赛项——任务2:离线数据处理

将下发的ds_db01.sql数据库文件放置mysql中 12、编写Scala代码&#xff0c;使用Spark将MySQL的ds_db01库中表user_info的全量数据抽取到Hive的ods库中表user_info。字段名称、类型不变&#xff0c;同时添加静态分区&#xff0c;分区字段为etl_date&#xff0c;类型为String&am…