微信小程序的自定义TabBar及Vant的使用

一、安装Vant

1、在 资源管理器 空白位置,点右键打开 在外部终端窗口打开

2、初始化NPM

npm init -y

3、安装命令

npm i @vant/weapp@1.3.3 -S --production

4、构建NPM包

在 工具 里选择构建NPM包

5、删除style:v2

在app.json里,删除"style":"v2"

6、按需引入

javascript 复制代码
// app.json
"usingComponents": {
     "van-button": "@vant/weapp/button/index"
}

7、使用

html 复制代码
<van-button type="primary">按钮</van-button>

二、CSS自定义样式

1、定义

css 复制代码
element {
    --main-bg-color: brown;
}
css 复制代码
:root {
    --main-bg-color: brown;
}

2、使用

css 复制代码
element {
    background-color: var(--main-bg-color: brown);
}

三、自定义TabBar

1、配置

在app.json中的 tabBar 配置中加上 "custom": true, 表示要自定义,不再使用list

为了兼容低版本,list虽然作废,但是也不能删

css 复制代码
  "tabBar": {
    "custom": true,
    "list": [
      {
    ....

2、新建自定义文件

在根目录下新建 custom-tab-bar文件夹,在里面新建index文件

3、引入vant依赖

vant中的tabBar组件介绍:

Vant Weapp - 轻量、可靠的小程序 UI 组件库

4、基础tabBar

vant中的基本tabBar代码

html 复制代码
<van-tabbar active="{{ active }}" bind:change="onChange">
  <van-tabbar-item icon="home-o">标签</van-tabbar-item>
  <van-tabbar-item icon="search">标签</van-tabbar-item>
  <van-tabbar-item icon="friends-o">标签</van-tabbar-item>
  <van-tabbar-item icon="setting-o">标签</van-tabbar-item>
</van-tabbar>

配套的JS代码

javascript 复制代码
Page({
  data: {
    active: 0,
  },
  onChange(event) {
    // event.detail 的值为当前选中项的索引
    this.setData({ active: event.detail });
  },
});

自定义图标的使用

icon : 表示未选中 时 显示的图标

icon-active : 表示选中 时 显示的图标

info="3" : 表示图标右上的小数字

html 复制代码
<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>

配套的JS

javascript 复制代码
Page({
  data: {
    active: 0,
    icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
    },
  },
  onChange(event) {
    this.setData({ active: event.detail });
  },
});

最后,把标签的名字和图片都写在list数组里面

javascript 复制代码
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"
      }
    ]
  }

使用时

html 复制代码
<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>

在自定义组件中使用 Vant Weapp 组件时,需在 options 开启 styleIsolation: 'shared' 选项,不然修改的样式不会生效

实现跳转Tab功能

javascript 复制代码
    onChange(event) {
      // event.detail 的值为当前选中项的索引
      // this.setData({ active: event.detail })
      this.updateActive(event.detail)
      wx.switchTab({
        url: this.data.list[event.detail].pagePath,
      })
    },
相关推荐
Dignity_呱1 小时前
如何在不发版时,实现小程序的 AB 测试?
前端·面试·微信小程序
说私域2 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa10 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184117 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售21 小时前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl1 天前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域1 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_1 天前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务1 天前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠1 天前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链