admin 发表于 2026-1-5 17:22:00

uniapp动态设置tabbar

首页代码:

// 动态设置tabBar第一项
                        // #ifdef APP || WEB
                        let tabIndex = getApp().globalData.tabIndex;
                        console.log(tabIndex)
                        if (tabIndex != 0) {
                                // #ifdef APP || WEB
                                let tabBar = {
                                        index: 0,
                                        selectedIconPath: "/static/tabbar/default.png",
                                        text: ''
                                }
                                // #endif
                                // #ifdef MP
                                let tabBar = {
                                        index: 0,
                                        text: '首页',
                                        iconPath: "/static/tabbar/home.png",
                                        selectedIconPath: "/static/tabbar/home-selected.png",
                                }
                                // #endif
                                uni.setTabBarItem(tabBar)
                                getApp().globalData.tabIndex = 0;
                        }
                        // #endif分类页代码:
onLoad() {
                        // 动态设置tabBar第一项
                        // uni.setTabBarItem({
                        //         index: 0,
                        //         text: '首页',
                        //         iconPath: "/static/tabbar/home.png",
                        //         selectedIconPath: "/static/tabbar/home-selected.png",
                        // })
                },
                onShow() {
                        // 动态设置tabBar第一项
                        // #ifdef APP || WEB
                        let tabIndex = getApp().globalData.tabIndex;
                        console.log(tabIndex)
                        if (tabIndex == 0) {
                                let tabBar = {
                                        index: 0,
                                        text: '首页',
                                        iconPath: "/static/tabbar/home.png"
                                }
                                uni.setTabBarItem(tabBar)
                        }
                        getApp().globalData.tabIndex = 1;
                        // #endif
                       
                },订单页代码:
onShow() {
                        // 动态设置tabBar第一项
                        // #ifdef APP || WEB
                        let tabIndex = getApp().globalData.tabIndex;
                        console.log(tabIndex)
                        if (tabIndex == 0) {
                                let tabBar = {
                                        index: 0,
                                        text: '首页',
                                        iconPath: "/static/tabbar/home.png"
                                }
                                uni.setTabBarItem(tabBar)
                        }
                        getApp().globalData.tabIndex = 2;
                        // #endif
                       
                },我的页面代码:
onShow() {
                        // 动态设置tabBar第一项
                        // #ifdef APP || WEB
                        let tabIndex = getApp().globalData.tabIndex;
                        console.log(tabIndex)
                        if (tabIndex == 0) {
                                let tabBar = {
                                        index: 0,
                                        text: '首页',
                                        iconPath: "/static/tabbar/home.png"
                                }
                                uni.setTabBarItem(tabBar)
                        }
                        getApp().globalData.tabIndex = 3;
                        // #endif
                       
                },

页: [1]
查看完整版本: uniapp动态设置tabbar