【愚公系列】2022年11月 微信小程序-Vant实现自定义tabBar

举报
愚公搬代码 发表于 2022/11/29 20:40:46 2022/11/29
【摘要】 前言小程序自定义tabBar官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html1、说明小程序自定义tabBar两种方式:一种采用官方自定义tabbar的方式,但是官方的自定义tabbar实际上也存在不少bug,比如某些情况下真机显示了两层。一种完全自己去实现,其...

前言

小程序自定义tabBar官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html

在这里插入图片描述
1、说明

小程序自定义tabBar两种方式:一种采用官方自定义tabbar的方式,但是官方的自定义tabbar实际上也存在不少bug,比如某些情况下真机显示了两层。一种完全自己去实现,其实就是用fixed定位写一个,但是在页面切换的时候会有闪动的问题,因为不同的页面都要引入自定义tabbar这个组件,微信小程序里不同页面的组件并不会完全共用。

2、配置使用

官方自定义tabbar配置方式:在app.json中的tabBar字段里配置custom字段值为true,在代码根目录添加custom-tab-bar组件文件。

一、Vant实现自定义tabBar

1.app.js配置

"tabBar": {
  "custom": true,
},

2.全局数据共享

// 在这个 JS 文件中,专门来创建 Store 的实例对象
import { observable, action } from 'mobx-miniprogram'

export const store = observable({
  activeTabBarIndex: 0,
  updateActiveTabBarIndex: action(function(index) {
    this.activeTabBarIndex = index
  })
})

3.custom-tab-bar组件

// custom-tab-bar/index.js
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../store/store'

Component({
  options: {
    styleIsolation: 'shared'
  },
  behaviors: [storeBindingsBehavior],
  storeBindings: {
    store,
    fields: {
      sum: 'sum',
      active: 'activeTabBarIndex'
    },
    actions: {
      updateActive: 'updateActiveTabBarIndex'
    },
  },
  observers: {
    'sum': function (val) {
      this.setData({
        'list[1].info': val
      })
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    "list": [
      {
        "pagePath": "/pages/home/home",
        "text": "首页",
        "iconPath": "/images/tabs/home.png",
        "selectedIconPath": "/images/tabs/home-active.png"
      },
      {
        "pagePath": "/pages/message/message",
        "text": "消息",
        "iconPath": "/images/tabs/message.png",
        "selectedIconPath": "/images/tabs/message-active.png",
        info: 0
      },
      {
        "pagePath": "/pages/contact/contact",
        "text": "联系我们",
        "iconPath": "/images/tabs/contact.png",
        "selectedIconPath": "/images/tabs/contact-active.png"
      }
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      // event.detail 的值为当前选中项的索引
      // this.setData({ active: event.detail })
      this.updateActive(event.detail)
      wx.switchTab({
        url: this.data.list[event.detail].pagePath,
      })
    },
  }
})

<!--custom-tab-bar/index.wxml-->
<van-tabbar active="{{active}}" bind:change="onChange" active-color="#13A7A0">
	<van-tabbar-item wx:for="{{list}}" wx:key="index" info="{{item.info ? item.info : ''}}">
		<image slot="icon" src="{{item.iconPath}}" mode="aspectFit" style="width: 25px; height: 25px;" />
		<image slot="icon-active" src="{{item.selectedIconPath}}" mode="aspectFit" style="width: 25px; height: 25px;" />
		{{item.text}}
	</van-tabbar-item>
</van-tabbar>

在这里插入图片描述

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。