微信小程序:19.TabBar 红点提醒解决方案

TabBar 红点提醒,很多小程序都需要这个功能比如聊天小程序,电商小程序等
这时候我们需要进行自定义 TabBar

配置信息

更改 custom 为 True 变为自定义 Tabbar

{
  "pages": [
    "pages/home/home",
    "pages/index/index",
    "pages/logs/logs"
  ],
  "window": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "Weixin",
    "navigationBarBackgroundColor": "#ffffff"
  },
  "componentFramework": "glass-easel",
  "sitemapLocation": "sitemap.json",
  "lazyCodeLoading": "requiredComponents",
  "usingComponents": {
    "van-button": "@vant/weapp/button/index",
    "my-numbers": "./components/numbers/numbers"
  },
  "tabBar": {
    "custom": true,
    "color": "#000000",
    "selectedColor": "#000000",
    "backgroundColor": "#ffffff",
    "list": [{
      "pagePath": "pages/home/home",
      "text": "组件"
    }, {
      "pagePath": "pages/index/index",
      "text": "接口"
    }]
  }
}

配置好后你会发现没有出现 Tabbar 这个是正常的

添加代码文件

在跟目录中创建文件夹和组件结构
CleanShot 2024-05-14 at 15.18.24.png
创建完成后可以看到这样的界面效果
CleanShot 2024-05-14 at 15.20.37.png
可以看到这块是一个自定义组建,如果不能出现该效果,可能是因为代码基础调试库的问题,通常在设置自定义 TabBar 提示 TypeError,这时候需要在 详情-》本地设置-》修改基础调试库,不要使用灰度测试版本,我这里实用的是 3.4.2 版本没有问题

实用 Vant 组建 TabBar

引用

"usingComponents": {
  "van-tabbar": "@vant/weapp/tabbar/index",
  "van-tabbar-item": "@vant/weapp/tabbar-item/index"
}

设置 Tabbar 样式

<!--custom-tab-bar/index.wxml-->
<van-tabbar active="{{ active }}" bind:change="onChange">
  <van-tabbar-item info="3">
    <image
      slot="icon"
      src="{{ icon.normal }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
      />
      <image
        slot="icon-active"
        src="{{ icon.active }}"
        mode="aspectFit"
        style="width: 30px; height: 18px;"
        />
        自定义
      </van-tabbar-item>
        <van-tabbar-item icon="search">标签</van-tabbar-item>
        <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
      </van-tabbar>

CleanShot 2024-05-14 at 15.30.45.png

设置 JS

// custom-tab-bar/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 0,
    icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event) {
      this.setData({ active: event.detail });
    }
  }
})

共享数据给 info 属性

创建 store 文件夹-》创建 storejs 文件

// 在这个 js 文件中专门创建 store 对象
import {observable,action} from 'mobx-miniprogram'

export const store = observable({
  numA:1,
  numB:2,
  info:3,
  //计算属性
  get sum(){
    return this.numA+this.numB
  },
  //action方法用来修改 store 中的值
  updateNum1:action(function(step){
    this.numA+=step
  }),
  updateNum2:action(function(step){
    this.numB+=step
  }),
})

设置 customjs 结构

import { storeBindingsBehavior } from 'mobx-miniprogram-bindings';
import { store } from '../store/store';

Component({
  behaviors: [storeBindingsBehavior],
  properties: {},
  storeBindings: {
    store,
    fields: {
      numA: () => store.numA,
      numB: () => store.numB,
      sum: 'sum'
    },
    actions: {
      buttonTap: 'update'
    }
  },
  data: {
    info: 0,
    active: 0,
    icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
    }
  },
  observers: {
    'sum': function(val) {
      this.setData({
        info: val
      });
    }
  },
  methods: {
    myMethod() {
      this.setData({
        info: this.data.sum
      });
    }
  }
});

设置 wxml 结构

<!--custom-tab-bar/index.wxml-->
<van-tabbar active="{{ active }}" bind:change="onChange">
  <van-tabbar-item info="{{numA}}">
    <image
      slot="icon"
      src="{{ icon.normal }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    />
    <image
      slot="icon-active"
      src="{{ icon.active }}"
      mode="aspectFit"
      style="width: 30px; height: 18px;"
    />
    自定义
  </van-tabbar-item>
  <van-tabbar-item icon="search">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
</van-tabbar>

这时候就可以进行加载
在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饭一口口吃

来杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值