uni-app 开发微信小程序 自定义tabBar

本文详细介绍了如何在uni-app中配置并实现自定义tabBar,包括在pages.js中的设置和创建custom-tab-bar组件,以及在index.wxml和index.js中的实现。同时,讲解了如何处理页面加载时的缓存问题,确保tabBar选中状态的正确显示。涉及到的技术点包括微信小程序开发、uni-app的自定义组件和数据绑定。

pages.js 里面tabBar配置加上"custom": true

"tabBar": {
	"custom": true,
    "color": "#8a8a8a",
    "selectedColor": "#198cfb",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [{
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "static/tabbar/basics.png",
        "selectedIconPath": "static/tabbar/basics_blue.png"
      },
      {
        "pagePath": "pages/UserCenter/index",
        "text": "我的",
        "iconPath": "static/tabbar/about.png",
        "selectedIconPath": "static/tabbar/about_blue.png"
      }
    ]
  }

然后在根目录下新增custom-tab-bar  因为uni-app 不会编译这个文件夹所以要建成微信小程序一样的

 主要的两个文件index.wxml,index.js

<view class="tab-bar-wrapper">
	<view class="tab-bar">
		<view class="tab-box">
			<view class="tab-bar-item" wx:for="{{list}}" wx:key="index" class="tab-bar-item"
				data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
				<image src="{{selected === index ? item.selectedIconPath : item.iconPath}}" mode=""></image>
				<view style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</view>
			</view>
		</view>
	</view>
</view>

index.js

Component({
  data: {
    selected: 0,
    color: "#ffffff",
    selectedColor: "#f40918",
    list: [{
      "pagePath": "/pages/index/index",
      "text": "首页",
      "iconPath": "/static/tabbar/basics.png",
      "selectedIconPath": "/static/tabbar/basics_blue.png"
    },{
      "pagePath": "/pages/UserCenter/index",
      "text": "我的",
      "iconPath": "/static/tabbar/about.png",
      "selectedIconPath": "/static/tabbar/about_blue.png"
    }]
  },
	attached () {
		this.setData({
		  selected: 0
		})
	},
	observers: {
		"selected": function (newVal) {
			const index = wx.getStorageSync("tabBarIndex")
			if (newVal != index) {
				this.setData({
				  selected: index
				})
			}
		}
	},
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({ url })
			const index = wx.getStorageSync("tabBarIndex")
      this.setData({
        selected: index
      })
			wx.setStorageSync("tabBarIndex", data.index)
    }
  }
})

在pages/index/index里面的onLoad,onShow方法中 uni.setStorageSync("tabBarIndex", 0) 缓存对应的值

pages/UserCenter/index uni.setStorageSync("tabBarIndex", 1) 

在main.js 中 uni.setStorageSync("tabBarIndex", 0)  这个是在页面初始化的时候将缓存值改为0  不然会显示上一次的缓存值

效果图

样式我没有贴出来 样式就自己改成ui图一样的就好 

 

<think>我们正在讨论uni-app开发微信小程序自定义底部tab组件的方法。根据引用内容,特别是引用[3][4][5],我们可以了解到uni-app支持自定义底部导航栏(tabBar),并且需要特定的配置步骤。 关键点在于在`tabBar`配置中设置`"custom": true`,然后创建一个自定义组件(通常放在项目根目录的`custom-tab-bar`文件夹中)来实现自定义tabBar。 步骤总结如下: 1. 在`pages.json`中配置`tabBar`,设置`"custom": true`,并保留原有的`list`配置(用于指定tabBar的页面路径和图标等)。 2. 在项目根目录下创建`custom-tab-bar`文件夹(必须是这个名称),并在其中创建组件(通常包括index.vue, index.js, index.json, index.wxss,或者如果是使用Vue的话,可以是一个Vue组件)。 3. 在自定义组件中,根据需求设计UI和交互,并实现切换逻辑(使用`wx.switchTab`跳转,并管理当前选中的tab)。 注意:引用[4]和[5]提供了自定义组件的代码示例,其中主要包含: - data:包括selected(当前选中索引)、color(文字颜色)、selectedColor(选中时文字颜色)、list(tab项列表,与pages.json中的list对应)。 - 方法:switchTab,用于处理tab切换事件,通过`wx.switchTab`跳转到对应页面,并更新selected状态。 此外,引用[4]提到从uni-app官网下载案例并拷贝到项目中,所以如果遇到问题可以参考官方示例。 下面我们详细描述实现步骤: </think>在 uni-app 开发微信小程序时实现自定义底部 tab 组件,需遵循以下步骤(基于引用内容整理): ### 一、核心配置步骤 1. **启用自定义模式** 在 `pages.json` 中声明 `custom: true`(关键配置): ```json "tabBar": { "custom": true, // 启用自定义 "list": [...] // 保留原有路由配置 } ``` 2. **创建自定义组件** - 在项目根目录新建 `custom-tab-bar` 文件夹(**必须用此名称**) - 添加四个文件:`index.vue`(视图)、`index.js`(逻辑)、`index.json`(配置)、`index.wxss`(样式) 3. **组件逻辑实现**(参考引用[4][5]) ```javascript Component({ data: { selected: 0, // 当前选中项索引 color: "#7A7E83", // 默认颜色 selectedColor: "#3cc51f", // 选中颜色 list: [ // tab项配置(需与pages.json一致) { pagePath: "/pages/index/index", text: "首页", iconPath: "/static/tabs/home.png", selectedIconPath: "/static/tabs/home-active.png" }, // ...其他tab项 ] }, methods: { switchTab(e) { const data = e.currentTarget.dataset; wx.switchTab({ url: data.path }); // 切换页面 this.setData({ selected: data.index }); // 更新选中状态 wx.setStorageSync("tabBarIndex", data.index); // 持久化选中索引 } } }) ``` ### 二、关键注意事项 1. **图标路径处理** - 使用绝对路径如 `/static/tabs/home.png` - 需准备两套图标:默认态(`iconPath`)和选中态(`selectedIconPath`) 2. **状态同步机制**(引用[5]) ```javascript observers: { "selected": function(newVal) { const index = wx.getStorageSync("tabBarIndex"); if (newVal != index) this.setData({ selected: index }) } } ``` 通过本地存储跨页面同步选中状态,解决页面跳转后状态丢失问题。 3. **样式自定义技巧** - 在 `index.vue` 中使用 `position: fixed` 实现底部固定定位 - 通过 `v-for` 循环渲染 `list` 数据生成 tab 项 - 动态绑定样式类:`:class="{ active: selected === index }"` ### 三、常见问题解决 1. **切换闪动问题** 在 `attached` 生命周期初始化选中状态: ```javascript attached() { this.setData({ selected: wx.getStorageSync("tabBarIndex") || 0 }) } ``` 2. **图标不显示** - 检查路径是否正确(建议使用绝对路径) - 确认图片已放在 `static` 目录下 - 真机调试时需将图片加入 `微信开发者工具` → `详情` → `本地设置` → `勾选不校验合法域名` 3. **多端兼容方案** 使用 `#ifdef MP-WEIXIN` 条件编译,确保仅在小平台生效: ```vue <!-- index.vue --> <template> #ifdef MP-WEIXIN <div class="custom-tabbar">...</div> #endif </template> ``` > **实现效果**:自定义 tabBar 支持完全自由的 UI 设计(如特殊形状按钮、动画效果等),同时保持原生 tabBar 的页面切换性能。实际案例可参考 uni-app 官方示例仓库 [^4]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值