使小程序使用vant-tabbar组件时,遇到以下报错:Couldn’t found the ‘custom-tab-bar/index.json’ file relative to ‘app.json.json’
vant-tabbar是自定义tabBar,所以需要遵循以下规则
自定义 tabBar
基础库 2.5.0 开始支持,低版本需做兼容处理。
在自定义 tabBar 模式下
使用流程
示例
{
"tabBar": {"custom": true,"color": "#000000","selectedColor": "#000000","backgroundColor": "#000000","list": [{"pagePath": "pages/index/index"},{"pagePath": "pages/user/user"}]},"pages": ["pages/index/index", "pages/user/user","custom-tab-bar/index"],
}
在代码根目录下添加入口文件,务必注意,目录不能有错,tabbar目录下命名必须是index,跟pages是同级的,不然会报以下错误:Couldn’t found the ‘custom-tab-bar/index.json’ file relative to ‘app.json.json’
custom-tab-bar/index.ts
custom-tab-bar/index.json
custom-tab-bar/index.wxml
custom-tab-bar/index.less

custom-tab-bar/index.tsPage({/*** 页面的初始数据*/data: {active: 0,list: [{icon: 'home-o',text: '概况',url: '/pages/index/index'},{icon: 'user-o',text: '个人信息',url: '/pages/user/user'}]},
onLoad() {const page:any = getCurrentPages().pop();console.log(page);this.setData({active: this.data.list.findIndex(item => item.url === `/${page.route}`)});},
/*** tab点击事件* @param event */tabOnChange(event: any) {this.setData({ active: event.detail });wx.switchTab({url: this.data.list[event.detail].url})},})
custom-tab-bar/index.json{"component": true,"usingComponents": {"van-tabbar": "@vant/weapp/tabbar/index","van-tabbar-item": "@vant/weapp/tabbar-item/index"}
}
custom-tab-bar/index.wxml{ active }}" bind:change="tabOnChange">{ list }}" wx:key="index" icon="{{ item.icon }}">{{item.text}}
下一篇:咸阳平明发黄昏龙山头