首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐
2025年6月9日 星期一 6:59pm

【微信小程序】page.json配置-tabBar底部栏

  • 25-01-18 12:02
  • 2080
  • 13691
blog.csdn.net

在微信小程序中,page.json 文件不仅用于配置单个页面的样式和其他属性,还可以用于配置底部导航栏(tabBar)。通过在 page.json 文件中配置 tabBar,可以实现小程序底部导航栏的自定义,包括导航栏的样式、图标和文字等。本文将详细介绍如何在 page.json 文件中配置底部导航栏,并提供一些最佳实践。

1. tabBar 基本配置

在 page.json 文件中配置 tabBar 时,需要在文件中添加 tabBar 属性,并指定相应的配置项。以下是详细的配置步骤和示例:

1.1 配置 tabBar 基本属性

在 page.json 文件中添加 tabBar 属性,并配置基本属性:

{
  "tabBar": {
    "color": "#7A7E83",               // 默认字体颜色
    "selectedColor": "#1296db",       // 选中字体颜色
    "borderStyle": "black",           // tabBar 上边框的颜色,仅支持 black / white
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/images/home.png",
        "selectedIconPath": "/images/home-active.png"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志",
        "iconPath": "/images/logs.png",
        "selectedIconPath": "/images/logs-active.png"
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2. 配置 tabBar 详细属性

除了基本属性外,tabBar 还支持更多详细的配置项,包括背景色、位置、自定义样式等。

2.1 背景色和位置

配置 tabBar 的背景色和位置:

{
  "tabBar": {
    "color": "#7A7E83",
    "selectedColor": "#1296db",
    "borderStyle": "black",
    "backgroundColor": "#FFFFFF",     // tabBar 背景色
    "position": "bottom",             // tabBar 位置,支持 bottom / top
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/images/home.png",
        "selectedIconPath": "/images/home-active.png"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志",
        "iconPath": "/images/logs.png",
        "selectedIconPath": "/images/logs-active.png"
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
2.2 自定义样式

如果需要自定义 tabBar 的样式,可以在 app.json 文件中配置 custom 属性,并在相应页面的 .json 文件中声明 usingComponents:

{
  "tabBar": {
    "custom": true,                   // 开启自定义 tabBar
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "/images/home.png",
        "selectedIconPath": "/images/home-active.png"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志",
        "iconPath": "/images/logs.png",
        "selectedIconPath": "/images/logs-active.png"
      }
    ]
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在每个页面的 .json 文件中声明 usingComponents:

{
  "component": true,
  "usingComponents": {
    "custom-tab-bar": "/components/custom-tab-bar/custom-tab-bar"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 自定义 tabBar 组件

如果需要完全自定义 tabBar 的样式和行为,可以创建一个自定义组件。以下是一个简单的自定义 tabBar 组件示例:

3.1 创建自定义组件

在项目根目录下创建 custom-tab-bar 目录,并在其中创建组件文件:

- custom-tab-bar/
  - index.js
  - index.json
  - index.wxml
  - index.wxss
  • 1
  • 2
  • 3
  • 4
  • 5
3.1.1 index.js
Component({
  properties: {
    selected: {
      type: Number,
      value: 0
    }
  },
  methods: {
    switchTab(e) {
      const idx = e.currentTarget.dataset.index;
      const path = e.currentTarget.dataset.path;
      wx.switchTab({ url: path });
      this.triggerEvent('change', { selected: idx });
    }
  }
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
3.1.2 index.json
{
  "component": true
}
  • 1
  • 2
  • 3
3.1.3 index.wxml
<view class="tab-bar">
  <view class="tab-item" wx:for="{{list}}" wx:key="index" data-index="{{index}}" data-path="{{item.pagePath}}" bindtap="switchTab">
    <image class="tab-icon" src="{{item.iconPath}}">image>
    <text class="tab-text" wx:class="{{selected === index ? 'active' : ''}}">{{item.text}}text>
  view>
view>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
3.1.4 index.wxss
.tab-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: #FFFFFF;
  padding: 10px 0;
  box-shadow: 0 -1px 6px rgba(0, 0, 0, 0.1);
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tab-icon {
  width: 24px;
  height: 24px;
}

.tab-text {
  font-size: 12px;
  color: #7A7E83;
}

.tab-text.active {
  color: #1296db;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

4. 使用自定义 tabBar 组件

在需要使用自定义 tabBar 的页面中,引入并使用自定义组件:

<template>
  <view>
    
    <custom-tab-bar selected="{{selected}}" bind:change="handleChange">custom-tab-bar>
  view>
template>

<script>
import customTabBar from '@/components/custom-tab-bar/custom-tab-bar';

export default {
  components: {
    customTabBar
  },
  data() {
    return {
      selected: 0,
      list: [
        {
          pagePath: '/pages/index/index',
          text: '首页',
          iconPath: '/images/home.png'
        },
        {
          pagePath: '/pages/logs/logs',
          text: '日志',
          iconPath: '/images/logs.png'
        }
      ]
    };
  },
  methods: {
    handleChange(e) {
      this.setData({
        selected: e.detail.selected
      });
    }
  }
};
script>

<style>
/* 页面样式 */
style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

5. 注意事项

  1. 组件路径: 确保组件路径正确无误,路径中的 / 是必需的。
  2. 组件命名: 组件名称应简洁明了,避免使用特殊字符和保留关键字。
  3. 分包问题: 如果组件位于分包中,确保分包已经正确配置,并且组件路径正确。
  4. 编译问题: 修改 tabBar 配置后,可能需要重新编译项目,以确保配置生效。
  5. 优先级: 自定义 tabBar 组件优先级高于默认 tabBar,如果有同名组件,自定义组件会优先被使用。

6. 最佳实践

  1. 统一管理: 将所有自定义组件集中管理在一个目录下,方便维护和查找。
  2. 命名规范: 组件名称应简洁明了,避免使用容易引起歧义的名称。
  3. 模块化设计: 将复杂的功能模块化,每个组件负责单一功能,提高代码的可复用性和可维护性。
  4. 文档记录: 记录每个组件的使用方法和注意事项,便于团队协作和后续维护。
  5. 性能优化: 合理配置 tabBar 模式,避免不必要的组件加载,优化页面性能。

通过合理配置 tabBar 和使用自定义组件,可以显著提升微信小程序的用户体验和开发效率。希望本文对你有所帮助,如果有任何问题或需要进一步的帮助,请随时留言交流。?

注:本文转载自blog.csdn.net的墨夶的文章"https://blog.csdn.net/2401_88677290/article/details/143643425"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
游戏

Python版贪吃蛇保姆级教程

Python版贪吃蛇(最后附上完整代码并有详细注释):从零开始用Pygame实现经典游戏一、前言:童年经典游戏重生贪吃蛇作为一款经典游戏,承载着无数人的童年回忆。今天我们将用Python+pygame从零开始实现这个经典游戏!通过本教程,你...
2025-04-25 3704 11984
游戏

python3.x 安装pygame常见报错即处理方法

Pygame 安装报错$ sudopip3install pygameerror: externally-managed-environment× This environment is externally managed╰─>To in...
2025-04-25 2439 14191
游戏

【unity的一些快捷键】

Unity中的快捷键可以帮助你极大地提高工作效率。以下是一些常用的Unity快捷键:文件操作:Ctrl + N:新建项目。Ctrl + S:保存场景或资产。Ctrl + Shift + S:保存所有场景和资产。视图控制:Ctrl + 0:重...
2025-04-25 3348 12305
游戏

【Unity基础】Unity中角色动画的三种实现方式

在Unity中,角色动画有三种不同的实现方式:逐帧动画(Frame-by-Frame)、剪裁动画(Cutout)和骨骼动画(Skeletal),各自适用于不同的场景和需求。以下是它们的核心区别及特点:1. 逐帧动画(Frame-by-Fra...
2025-04-25 3523 9597
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

  • 1

    Python版贪吃蛇保姆级教程

  • 2

    python3.x 安装pygame常见报错即处理方法

  • 3

    【unity的一些快捷键】

  • 4

    【Unity基础】Unity中角色动画的三种实现方式

  • 5

    Unity6000下载地址

  • 6

    10 个pygame经典小游戏

  • 7

    【逆向工程】破解unity的安卓apk包

  • 8

    unity 无法安装6000版本的问题

  • 9

    Pico 4 Enterprise(企业版)与Unity的交互-打包运行及UI交互篇

  • 10

    从零开始打造属于你的Pygame小游戏:轻松入门与高效开发

134
游戏
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top