JavaScript

uniapp微信小程序Vue3解决自定义tabbar的问题

2023-03-17  本文已影响0人  剑指流云

先上关键词,方便大家搜索

先配置page.json,这里配置了两套Tabbar,一个给微信小程序使用,一个给别的平台使用

// #ifdef MP-WEIXIN
    "tabBar": {
        "selectedColor": "#47A1FF",
        "custom": true,
        "list": [{
                "pagePath": "pages/index/index",
                "text": "抢单大厅",
                "iconPath": "/static/index-light.png",
                "selectedIconPath": "/static/index.png"
            },
            {
                "pagePath": "pages/worker/worker",
                "text": "工作台",
                "iconPath": "/static/menu.png",
                "selectedIconPath": "/static/menu.png"
            },
            {
                "pagePath": "pages/mine/mine",
                "text": "我的",
                "iconPath": "/static/mine-light.png",
                "selectedIconPath": "/static/mine.png"

            }
        ]
    },
    //  #endif
    // #ifndef MP-WEIXIN
    "tabBar": {
        "selectedColor": "#47A1FF",
        "backgroundColor": "#fff",
        "midButton": {
            "pagePath": "pages/worker/worker",
            "text": "工作台",
            "iconPath": "/static/menu.png",
            "selectedIconPath": "/static/menu.png"
        },
        "list": [{
                "pagePath": "pages/index/index",
                "text": "抢单大厅",
                "iconPath": "/static/index-light.png",
                "selectedIconPath": "/static/index.png"
            },
            {
                "pagePath": "pages/worker/worker",
                "text": "工作台",
                "iconPath": "/static/menu.png",
                "selectedIconPath": "/static/menu.png"
            },
            {
                "pagePath": "pages/mine/mine",
                "text": "我的",
                "iconPath": "/static/mine-light.png",
                "selectedIconPath": "/static/mine.png"

            }
        ]
    },
    //  #endif

在微信小程序的官方文档中找到微信的自定义Tabbar示例,代码在文档里最下方

注意以下问题

// #ifdef MP-WEIXIN 
import  {onShow}  from "@dcloudio/uni-app"
onShow(()=>{
    const curPages = getCurrentPages()[0];  // 获取当前页面实例
    if (typeof curPages.getTabBar === 'function' && curPages.getTabBar()) {  
        curPages.getTabBar().setData({  
            selected: 2   // 表示当前菜单的索引,该值在不同的页面表示不同
        });  
    }
})
// #endif
上一篇 下一篇

猜你喜欢

热点阅读