微信小程序引入vant tabbar不显示问题

项目配置了app.json以开启自定义tabbar,并全局引入vant-tabbar组件。同时,在custom-tab-bar文件夹下创建index.js和index.wxml,定义tabbar的样式和行为。每个页面的onShow方法中调用初始化tabbar。注意,小程序默认首页不能放在pages数组的第一个位置,否则会导致tabbar不显示。
摘要由CSDN通过智能技术生成
  1. 项目目录结构
    在这里插入图片描述

  2. app.json

  • tabbar开启custom:true自定义tabbar
  • 在app.json中引入vant-tabbar(这里是全局引入,也可以在需要的page中的json文件中单独引入)
{
    "pages": [
        "pages/tab1/search",
        "pages/tab2/about",
        "pages/index/index"
    ],
    "window": {
        "backgroundTextStyle": "light",
        "navigationBarBackgroundColor": "#fff",
        "navigationBarTitleText": "Weixin",
        "navigationBarTextStyle": "black"
    },
"tabBar": {
  "custom": true,
  "color": "#1B1B1B",
  "selectedColor": "#00c56b",
  "borderStyle": "black",
  "backgroundColor": "#fafbfe",
  "list": [ 
    {
      "pagePath": "pages/tab1/search",
      "text": "搜索"
    },
    {
      "pagePath": "pages/tab2/about",
      "text": "我的"
    }
  ]
},
"usingComponents": { 
  "van-tabbar": "/miniprogram_npm/vant-weapp/tabbar/index",
  "van-tabbar-item": "/miniprogram_npm/vant-weapp/tabbar-item/index"
},
  "sitemapLocation": "sitemap.json"
}
  1. 在项目根目录下新建custom-tab-bar(文件夹名称必须为这个),并新建index的component
    在这里插入图片描述
  2. custom-tab-bar/index.js(list由自己定义
Component({
  data: { 
    selected: 0,
    list: [
      {
        pagePath: "/pages/tab1/search",
        text: "运动咨询",
        icon: "smile-o",

      },
      {
        pagePath: "/pages/tab2/about",
        text: "个人中心",
        icon:"user-o",
      },

    ]
  },
  methods: {
    onChange(e) {
       console.log(e,'e')
       this.setData({ active: e.detail });
       wx.switchTab({
         url: this.data.list[e.detail].pagePath
       });
    },
    init() {
        const page = getCurrentPages().pop();
        this.setData({
          active: this.data.list.findIndex(item => item.pagePath === `/${page.route}`)
        });
       }
    }
})

  1. custom-tab-bar/index.wxml
<van-tabbar active="{{ active }}" bind:change="onChange" active-color="#07c160">
  <van-tabbar-item wx:for="{{ list }}" icon="{{ item.icon }}" wx:key="index" >
    {{item.text}}
  </van-tabbar-item>
</van-tabbar>
  1. 在每个切换页的js文件中添加onshow方法
    在这里插入图片描述
methods: {
     /**
   * 生命周期函数--监听页面显示
   */
  onShow:function() {
    this.getTabBar().init();
  },
  }
  1. tabbar不显示问题说明
    app.json中pages中的小程序默认首页不能放在第一个,否则无法显示tabbar
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值