原生小程序用vant组件自定义底部导航

在根目录中创建custom-tab-bar,新建page index
app.jsonindex.json中引入vant组件
"usingComponents": {
  "van-tabbar": "@vant/weapp/tabbar/index",
  "van-tabbar-item": "@vant/weapp/tabbar-item/index"
}

index.wxss
/* custom-tab-bar/index.wxss */
.custom-tab-bar-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.custom-tab-bar-wrapper .text {
  font-size: 24rpx;
}

.custom-tab-bar-wrapper .tab-icon {
  width: 40rpx;
  height: 40rpx;
}

.custom-tabbar-item {
  width: 100% !important;
}
.imageIcon{
  width: 50rpx; height: 50rpx;
}

index.wxml
<van-tabbar 
  active="{{ active }}" 
  bind:change="onChange"
  inactive-color="#333333"
  active-color="#1479FF"
>
  <van-tabbar-item class="custom-tab-bar-wrapper" custom-class="custom-tabbar-item" wx:for="{{list}}" wx:key="index">
    <image
      slot="icon"
      src="{{ item.iconPath }}"
      mode="aspectFit"
      class="imageIcon"
    />
    <image
      slot="icon-active"
      src="{{ item.selectedIconPath }}"
      mode="aspectFit"
      class="imageIcon"
    />
    <view class="text">{{ item.text }}</view>
  </van-tabbar-item>
</van-tabbar>

index.js
// custom-tab-bar/index.js
Component({
  data: {
    active: 0,
    list: [
      {
        pagePath: "pages/home/index",
        text: "首页",
        iconPath: "/static/images/home.png",
        selectedIconPath: "/static/images/home_active.png"
      },
      {
        pagePath: "pages/diagnosis/index",
        text: "分类",
        iconPath: "/static/images/classify.png",
        selectedIconPath: "/static/images/classify_active.png"
      },
      {
        pagePath: "pages/personal_center/index",
        text: "我的",
        iconPath: "/static/images/personal.png",
        selectedIconPath: "/static/images/personal_active.png"
      }
    ]
  },
  methods: {
    onChange(event) { // 切换tab页面
      this.setData({ active: event.detail });
      // console.log(this.data.list[event.detail].pagePath)
      wx.switchTab({
        url: this.data.list[event.detail].pagePath.startsWith('/')
          ? this.data.list[event.detail].pagePath
          : `/${this.data.list[event.detail].pagePath}`,
      });
    },

    init() { // 设置tab的active下标(icon)
      const page = getCurrentPages().pop();
      const route = page ? page.route.split('?')[0] : '';
      const active = this.data.list.findIndex(
        (item) =>
          (item.pagePath.startsWith('/') ? item.pagePath.substr(1) : item.pagePath) ===
          `${route}`,
      );
      this.setData({ active });
    },
  },
});


app.json
 "tabBar": {
    "custom": true,
    "color": "#333",
    "selectedColor": "#333333",
  },
在tab页中的onShow()
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().init() // 设置tabbar active状态
  },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值