uniapp动态底部tab栏

实现思路:

创建一个js文件用来存放所有的tabbar,不同的数组表示不同的tabbar组合。

创建一个vue文件用来制作底部tabbar组件。

使用vuex存储用户的身份信息,根据身份信息切换tabbar组合。

具体步骤:

新建一个tabbar.js文件,将不同的tabbar组合用不同的数组表示出来。

javascript 复制代码
// 公共页面
export const publicBar = [{
        "pagePath": "/pages/home/index",
        "iconPath": require("@/static/logo.png"),
        "selectedIconPath": require("@/static/logo2.jpg"),
        "text": "首页"
    },
    {
        "pagePath": "/pages/car/index",
        "iconPath": require("@/static/logo.png"),
        "selectedIconPath": require("@/static/logo.png"),
        "text": "购物车"
    }
]
 
// 自己的页面
export const selfBar = [{
        "pagePath": "/pages/home/index",
        "iconPath": require("@/static/logo.png"),
        "selectedIconPath": require("@/static/logo.png"),
        "text": "首页"
    },
    {
        "pagePath": "/pages/mine/index",
        "iconPath": require("@/static/logo.png"),
        "selectedIconPath": require("@/static/logo.png"),
        "text": "我的"
    },
]

创建一个vue文件编写底部tabbar组件代码。

html 复制代码
<template>
    <view class="tabbar-list">
        <view class="tabbar-item" v-for="(item, index) in tabBarList" :key="index" @click="changeActive(item, index)">
            <image class="img" :src="activeIndex === index ? item.selectedIconPath : item.iconPath"></image>
            <view>{{ item.text }}</view>
        </view>
    </view>
</template>
 
<script>
    import {
        mapState,
        mapMutations
    } from 'vuex'
    export default {
        data() {
            return {}
        },
        computed: {
            ...mapState('tabBarModule', ['activeIndex', 'tabBarList']),
        },
        methods: {
            ...mapMutations('tabBarModule', ['setUserInfo', 'changeIndex']),
            // 修改点击的tabbar项
            changeActive(item, index) {
                this.changeIndex(index)
                uni.switchTab({
                    url: item.pagePath
                })
            },
        },
        mounted() {
            // 模拟登录后获取的用户信息,'public'为公共模块,非'public'为我的模块
            this.setUserInfo('public')
            // this.setUserInfo('mine') 用这个进行切换就能看到不同的底部tabbar
        }
    }
</script>
<style lang="scss" scoped>
    .tabbar-list {
        display: flex;
        position: fixed;
        bottom: 0;
        width: 100%;
        height: 100rpx;
        border: 1px solid #ccc;
        overflow: hidden;
 
        .tabbar-item {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
 
            .img {
                width: 50rpx;
                height: 50rpx;
            }
        }
    }
</style>

在根目录创建store文件夹,在store文件夹下创建module文件夹和创建index.js文件,在module文件夹下面创建tabBarModule.js文件。

在tabBarModule.js文件中编写vuex代码,然后在store文件夹下面的index.js文件中引入tabBarModule.js文件作为一个模块。

javascript 复制代码
// 引入两个tabbar组合
import {
    publicBar,
    selfBar
} from '@/utils/tabbar.js'
export default {
    // 开启命名空间
    namespaced: true,
    // 存放基础数据
    state: {
        // 用户信息
        userInfo: uni.getStorageSync('userInfo') || '',
        // 初始化一个空的tabbar组合
        tabBarList: [],
        // 当前选中的tabbar项,控制页面刷新导航高亮位置不变
        activeIndex: uni.getStorageSync('acIndex') || 0, 
    },
    mutations: {
        // 保存用户信息
        setUserInfo(state, token) {
            uni.setStorageSync('userInfo', token)
            state.userInfo = token;
            // 根据用户信息切换tabbar组合
            token !== 'public' ?
                state.tabBarList = selfBar :
                state.tabBarList = publicBar
 
        },
        // 记录当前选中的tabbar项
        changeIndex(state, index) {
            uni.setStorageSync('acIndex', index)
            state.activeIndex = index
        }
    },
}
在index.js文件中引入tabBarModule模块,并且在mian.js中引入store
import tabBarModule from '@/store/module/tabBarModule.js'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
    modules: {
        tabBarModule
    }
})

在每个页面引入刚才的底部tabbar组件,并且使用uni.hideTabBar()隐藏默认导航栏

附上page.json文件供参考

javascript 复制代码
{
    "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
        {
            "path": "pages/home/index",
            "style": {
                "navigationBarTitleText": "home"
            }
        },
        {
            "path": "pages/mine/index",
            "style": {
                "navigationBarTitleText": "mine"
            }
        },
        {
            "path": "pages/car/index",
            "style": {
                "navigationBarTitleText": "car"
            }
        }
    ],
    "globalStyle": {
        "navigationBarTextStyle": "black",
        "navigationBarTitleText": "uni-app",
        "navigationBarBackgroundColor": "#F8F8F8",
        "backgroundColor": "#F8F8F8"
    },
    "uniIdRouter": {},
    "tabBar": {
        "list": [
            {
                "pagePath": "pages/home/index"
            },
            {
                "pagePath": "pages/mine/index"
            },
            {
                "pagePath": "pages/car/index"
            }
        ]
    }
}

原文链接:https://blog.csdn.net/weixin_47190975/article/details/129353819

亲测有效,另外,点击退出后重新登录跳转首页activeIndex不会自动切换(即tab栏激活项未切换),在登录成功跳转首页前添加this.changeIndex(0)即可

相关推荐
CharlesYu018 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
Bs_MoneyMagnet8 小时前
基于springboot+vue的医疗健康便民服务平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring
平头哥技术团队8 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5
子兮曰9 小时前
Bun v1.4.1 深度解析:从 Zig 到 Rust,一场 11 天、64 个 AI 代理的语言迁徙
前端·后端·bun
人民广场吃泡面10 小时前
什么是AI Agent?它又能给前端带来哪些效率提升?
前端·人工智能
中科三方10 小时前
两家域名注册商资质被ICANN终止:企业域名资产安全再受关注
前端·网络·安全·域名
bug总结10 小时前
uniapp vue3全局方法注册使用
前端·javascript·uni-app
华无丽言11 小时前
如何在宜搭中实现获取子表中的字段值赋值到父表中?
前端·javascript·低代码
IT_陈寒11 小时前
Vue的computed属性竟然坑了我一把
前端·人工智能·后端
威斯软科的老司机11 小时前
通俗讲解 CNN 图像识别、向量 Embedding、Softmax 概率计算这三块的简化原理
前端·人工智能·ui·数字孪生