当前位置:   article > 正文

uniapp底部动态tabbar_uniapp底部导航栏动态

uniapp底部导航栏动态

案例git地址:asyncTabbar: uniapp动态tabbar案例

实现思路:

  1.         创建一个js文件用来存放所有的tabbar,不同的数组表示不同的tabbar组合。
  2.         创建一个vue文件用来制作底部tabbar组件。
  3.         使用vuex存储用户的身份信息,根据身份信息切换tabbar组合。

具体步骤:

  1.        新建一个tabbar.js文件,将不同的tabbar组合用不同的数组表示出来。
    1. // 公共页面
    2. export const publicBar = [{
    3. "pagePath": "/pages/home/index",
    4. "iconPath": require("@/static/logo.png"),
    5. "selectedIconPath": require("@/static/logo2.jpg"),
    6. "text": "首页"
    7. },
    8. {
    9. "pagePath": "/pages/car/index",
    10. "iconPath": require("@/static/logo.png"),
    11. "selectedIconPath": require("@/static/logo.png"),
    12. "text": "购物车"
    13. }
    14. ]
    15. // 自己的页面
    16. export const selfBar = [{
    17. "pagePath": "/pages/home/index",
    18. "iconPath": require("@/static/logo.png"),
    19. "selectedIconPath": require("@/static/logo.png"),
    20. "text": "首页"
    21. },
    22. {
    23. "pagePath": "/pages/mine/index",
    24. "iconPath": require("@/static/logo.png"),
    25. "selectedIconPath": require("@/static/logo.png"),
    26. "text": "我的"
    27. },
    28. ]

  2. 创建一个vue文件编写底部tabbar组件代码。
    1. <template>
    2. <view class="tabbar-list">
    3. <view class="tabbar-item" v-for="(item, index) in tabBarList" :key="index" @click="changeActive(item, index)">
    4. <image class="img" :src="activeIndex === index ? item.selectedIconPath : item.iconPath"></image>
    5. <view>{{ item.text }}</view>
    6. </view>
    7. </view>
    8. </template>
    9. <script>
    10. import {
    11. mapState,
    12. mapMutations
    13. } from 'vuex'
    14. export default {
    15. data() {
    16. return {}
    17. },
    18. computed: {
    19. ...mapState('tabBarModule', ['activeIndex', 'tabBarList']),
    20. },
    21. methods: {
    22. ...mapMutations('tabBarModule', ['setUserInfo', 'changeIndex']),
    23. // 修改点击的tabbar项
    24. changeActive(item, index) {
    25. this.changeIndex(index)
    26. uni.switchTab({
    27. url: item.pagePath
    28. })
    29. },
    30. },
    31. mounted() {
    32. // 模拟登录后获取的用户信息,'public'为公共模块,非'public'为我的模块
    33. this.setUserInfo('public')
    34. // this.setUserInfo('mine') 用这个进行切换就能看到不同的底部tabbar
    35. }
    36. }
    37. </script>
    38. <style lang="scss" scoped>
    39. .tabbar-list {
    40. display: flex;
    41. position: fixed;
    42. bottom: 0;
    43. width: 100%;
    44. height: 100rpx;
    45. border: 1px solid #ccc;
    46. overflow: hidden;
    47. .tabbar-item {
    48. flex: 1;
    49. display: flex;
    50. flex-direction: column;
    51. align-items: center;
    52. justify-content: center;
    53. .img {
    54. width: 50rpx;
    55. height: 50rpx;
    56. }
    57. }
    58. }
    59. </style>

  3. 在根目录创建store文件夹,在store文件夹下创建module文件夹和创建index.js文件,在module文件夹下面创建tabBarModule.js文件。                             
  4. 在tabBarModule.js文件中编写vuex代码,然后在store文件夹下面的index.js文件中引入tabBarModule.js文件作为一个模块。
    1. // 引入两个tabbar组合
    2. import {
    3. publicBar,
    4. selfBar
    5. } from '@/utils/tabbar.js'
    6. export default {
    7. // 开启命名空间
    8. namespaced: true,
    9. // 存放基础数据
    10. state: {
    11. // 用户信息
    12. userInfo: uni.getStorageSync('userInfo') || '',
    13. // 初始化一个空的tabbar组合
    14. tabBarList: [],
    15. // 当前选中的tabbar项,控制页面刷新导航高亮位置不变
    16. activeIndex: uni.getStorageSync('acIndex') || 0,
    17. },
    18. mutations: {
    19. // 保存用户信息
    20. setUserInfo(state, token) {
    21. uni.setStorageSync('userInfo', token)
    22. state.userInfo = token;
    23. // 根据用户信息切换tabbar组合
    24. token !== 'public' ?
    25. state.tabBarList = selfBar :
    26. state.tabBarList = publicBar
    27. },
    28. // 记录当前选中的tabbar项
    29. changeIndex(state, index) {
    30. uni.setStorageSync('acIndex', index)
    31. state.activeIndex = index
    32. }
    33. },
    34. }
  5. 在index.js文件中引入tabBarModule模块,并且在mian.js中引入store
    1. import tabBarModule from '@/store/module/tabBarModule.js'
    2. import Vue from 'vue'
    3. import Vuex from 'vuex'
    4. Vue.use(Vuex)
    5. export default new Vuex.Store({
    6. modules: {
    7. tabBarModule
    8. }
    9. })

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

  7. 附上page.json文件供参考 

    1. {
    2. "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
    3. {
    4. "path": "pages/home/index",
    5. "style": {
    6. "navigationBarTitleText": "home"
    7. }
    8. },
    9. {
    10. "path": "pages/mine/index",
    11. "style": {
    12. "navigationBarTitleText": "mine"
    13. }
    14. },
    15. {
    16. "path": "pages/car/index",
    17. "style": {
    18. "navigationBarTitleText": "car"
    19. }
    20. }
    21. ],
    22. "globalStyle": {
    23. "navigationBarTextStyle": "black",
    24. "navigationBarTitleText": "uni-app",
    25. "navigationBarBackgroundColor": "#F8F8F8",
    26. "backgroundColor": "#F8F8F8"
    27. },
    28. "uniIdRouter": {},
    29. "tabBar": {
    30. "list": [
    31. {
    32. "pagePath": "pages/home/index"
    33. },
    34. {
    35. "pagePath": "pages/mine/index"
    36. },
    37. {
    38. "pagePath": "pages/car/index"
    39. }
    40. ]
    41. }
    42. }

         有不理解的地方欢迎私信询问!!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/460489
推荐阅读
相关标签
  

闽ICP备14008679号