uniapp 小程序自定义tabbar及初次加载闪屏解决方案

很惭愧,这竟然是老衲在csdn的首次内容输出,还请看官,高抬贵手,多喷两句, 反正我也不听~👀

首先声明,我是最近才刚开始写uniapp……

言归正传,最近给公司要做一个小程序,由于之前并没有实际从0开始构建开发经验,故记录下遇到的一些小难点,方便之后查阅,希望也能帮到其他小朋友,目标项目主界面如下图所示

如果想实现这个效果的tabbar,那我们就只能舍弃小程序本身自带的了,在网上疯狂找了一圈,没有找到相对比较满意的解决办法,尝试后基本卡在首次加载切换时,每个页面都要初始化闪烁一下,不是很美丽,程序猿的强迫症怎么能允许!!最后结合查阅的资料结合自己的一些理解,算是比较完美的解决。

思路大概是这样

  • 首先封装一个自己用的tabbar组件

  • 然后配置page.js

  • 全局引用自定义tabbar

  • 每个tabbar页面引用组件

  • 最后进入主题:解决初次加载闪屏

话不多说,直接上代码……

  1. 首先我们先封装一个自定义的tabbar组件(配置信息自行根据业务更改)

<template>
  <view class="tabbar-container">
    <block>
      <view class="tabbar-item" v-for="(item, index) in tabbarList" :class="[item.centerItem ? ' center-item' : '']" @click="changeItem(item)">
        <view class="item-top"><image :src="currentItem == item.id ? item.selectIcon : item.icon"></image></view>
        <view class="item-bottom" :class="[currentItem == item.id ? 'item-active' : '']">
          <text>{{ item.text }}</text>
        </view>
      </view>
    </block>
  </view>
</template>
 
<script>
export default {
  props: {
    currentPage: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      currentItem: 0,
      tabbarList: [
        {
          id: 0,
          path: '/pages/index/index',
          icon: '/static/home.png',
          selectIcon: '/static/homeSelected.png',
          text: '简介',
          centerItem: false
        },
        {
          id: 1,
          path: '/pages/discount/discount',
          icon: '/static/gift.png',
          selectIcon: '/static/giftSelected.png',
          text: '优惠',
          centerItem: false
        },
        {
          id: 2,
          path: '/pages/code/code',
          icon: '/static/code.png',
          selectIcon: '/static/codeSelected.png',
          text: '二维码',
          centerItem: true
        },
        {
          id: 3,
          path: '/pages/search/search',
          icon: '/static/search.png',
          selectIcon: '/static/searchSelected.png',
          text: '探索',
          centerItem: false
        },
        {
          id: 4,
          path: '/pages/mine/mine',
          icon: '/static/mine.png',
          selectIcon: '/static/mineSelected.png',
          text: '我的',
          centerItem: false
        }
      ]
    };
  },
  mounted() {
    this.currentItem = this.currentPage;
    uni.hideTabBar();
  },
  methods: {
    changeItem(item) {
      let _this = this;
      //_this.currentItem = item.id;
      uni.switchTab({
        url: item.path
      });
    }
  }
};
</script>
<style>
view {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.tabbar-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 110rpx;
  box-shadow: 0 0 5px #999;
  display: flex;
  align-items: center;
  padding: 5rpx 0;
  color: #999999;
}
.tabbar-container .tabbar-item {
  width: 20%;
  height: 100rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
}
.tabbar-container .item-active {
  color: #f00;
}
.tabbar-container .center-item {
  display: block;
  position: relative;
}
.tabbar-container .tabbar-item .item-top {
  width: 70rpx;
  height: 70rpx;
  padding: 10rpx;
}
.tabbar-container .center-item .item-top {
  flex-shrink: 0;
  width: 100rpx;
  height: 100rpx;
  position: absolute;
  top: -50rpx;
  left: calc(50% - 50rpx);
  border-radius: 50%;
  box-shadow: 0 0 5px #999;
  background-color: #ffffff;
}
.tabbar-container .tabbar-item .item-top image {
  width: 100%;
  height: 100%;
}
.tabbar-container .tabbar-item .item-bottom {
  font-size: 28rpx;
  width: 100%;
}
.tabbar-container .center-item .item-bottom {
  position: absolute;
  bottom: 5rpx;
}
</style>

2.然后我们配置下page.js

{
  "pages": [{
    "path": "pages/index/index",
    "style": {
      "navigationBarTitleText": "简介"
    }
  }, {
    "path": "pages/discount/discount",
    "style": {
      "navigationBarTitleText": "优惠"
    }
 
  }, {
    "path": "pages/code/code",
    "style": {
      "navigationBarTitleText": "二维码"
    }
 
  }, {
    "path": "pages/search/search",
    "style": {
      "navigationBarTitleText": "探索"
    }
 
  }, {
    "path": "pages/mine/mine",
    "style": {
      "navigationBarTitleText": "我的"
    }
 
  }],
  "globalStyle": {
    "navigationBarTextStyle": "black",
    "navigationBarTitleText": "CRM",
    "navigationBarBackgroundColor": "#F8F8F8",
    "backgroundColor": "#F8F8F8",
    "app-plus": {
      "background": "#efeff4"
    }
  },
  "tabBar": {
    "color": "#999999",
    "selectedColor": "#f00",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "midButton":{
      "text":"二维码",
      "pagePath":"pages/code/code",
      "iconPath":"static/code.png",
      "selectedIconPath":"static/codeSelected.png"
    },
    "list":[
      {
        "pagePath":"pages/index/index",
        "iconPath":"static/home.png",
        "selectedIconPath":"static/homeSelected.png",
        "text":"简介"
      },
      {
        "pagePath":"pages/discount/discount",
        "iconPath":"static/gift.png",
        "selectedIconPath":"static/giftSelected.png",
        "text":"优惠"
      },
      {
        "pagePath":"pages/code/code",
        "iconPath":"static/code.png",
        "selectedIconPath":"static/codeSelected.png",
        "text":"二维码"
      },
      {
        "pagePath":"pages/search/search",
        "iconPath":"static/search.png",
        "selectedIconPath":"static/searchSelected.png",
        "text":"探索"
      },
      {
        "pagePath":"pages/mine/mine",
        "iconPath":"static/mine.png",
        "selectedIconPath":"static/mineSelected.png",
        "text":"我的"
      }
    ]
  }
}

3.注册全局组件tabbar在main.js文件中,配置如下:

import Vue from 'vue'
import App from './App'
import diyTabbar from "components/zdy-tabbar.vue"
 
// 注册全局组件
Vue.component('diy-tabbar', diyTabbar)
 
Vue.config.productionTip = false
 
App.mpType = 'app'
 
const app = new Vue({
  ...App
})
app.$mount()

4.所有的tabbar页面引入自定义tabbar:

<diy-tabbar :current-page="0"></diy-tabbar>  // current-page 对应的就是tabbar的index

至此,我们完成了第一步这个伟大的壮举,兴冲冲的启动程序,激动的心,颤抖的手点击第一个tabbar~

咦?是什么在闪烁呀,于是我们疯狂的点击了每一个却发现都在闪,当我们再次点击每个一个tabbar,又都好了...

于是我们心安理得的想,这样就可以了,毕竟整个这个确实没有原生的用着爽,老子已经尽力了....

然后只过了两分钟,我那份强迫症就犯了,这也太丑了,与我前端大神的身份不搭啊

对于初次加载闪屏问题的资料网上一大堆假的,这个有这么难吗?为啥连个像样的资料都找不到,...

于是只能自己造小三轮车了,思路就是

  • 创建一个主页面

  • 将所有tabbar组件和页面都引入其中,

  • 这样共用一个tabbar就不会出现闪屏的问题

这样就能稍微优雅的坐上三轮车,诶?不对,我在说什么...

1.首先,我们先建一个主页面,将所有tabbar页面引入

<template>
    <view>
        <view class="main_box">
            <index v-if="currentIndex === 0"></index>
            <message v-if="currentIndex === 1"></message>
            <!-- <midBtn v-if="currentIndex === 2"></midBtn> -->
            <member v-if="currentIndex === 3"></member>
            <my v-if="currentIndex === 4"></my>
        </view>
        <view class="foot_box">
            <diy-tabbar :current-page="currentIndex" @changeItem="changeItem"></diy-tabbar>
        </view>
        <u-popup class="firstPagePopup" :show="active" :closeable="true" @close="close" @open="open">
            <view>
                <view class="tabbar-box-wrap">
                    <view class="tabbar-box">
                        <view class="tabbar-box-item" @click="goToPage('/pages/tabbar-3-detial/tabbar-3-release/tabbar-3-release')">
                            <image class="box-image" src="@/static/img/mid_btn1.png" mode="aspectFit"></image>
                            <text class="explain">发文章</text>
                        </view>
                        <view class="tabbar-box-item" @click="goToPage('/pages/tabbar-3-detial/tabbar-3-video/tabbar-3-video')">
                            <image class="box-image" src="@/static/img/mid_btn2.png" mode="aspectFit"></image>
                            <text class="explain">发图文</text>
                        </view>
                        <view class="tabbar-box-item" @click="goToPage('/pages/tabbar-3-detial/tabbar-3-qa/tabbar-3-qa')">
                            <image class="box-image" src="@/static/img/mid_btn3.png" mode="aspectFit"></image>
                            <text class="explain">发视频</text>
                        </view>
                    </view>
                </view>
            </view>
        </u-popup>
    </view>
</template>

<script>
    import index  from '@/pages/index/index.vue'
    import my  from '@/pages/my/index.vue'
    import message  from '@/pages/message/index.vue'
    import member from '@/pages/memberCenter/index.vue'
    export default {
        components:{
            index,
            my,
            message,
            member
        },
        data() {
            return {
                active:false,
                currentIndex:4
            }
        },
        onLoad() {
        },
        methods: {
            // 如果
            changeItem(item){
                if(item.id === 2){
                    this.active = true
                }else{
                    this.currentIndex = item.id
                }
                // uni.switchTab({
                //   url: item.path
                // });
                // console.log(item)
            },
            close(){
                debugger
                this.active=false
            },
            open(){}
        }
    }
</script>

<style lang="scss" scoped>
.main_box{
    height: calc(100vh - 110rpx);
    overflow: scroll;
}
.foot_box{
    height: 110rpx;
}
.content {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    /* #ifdef H5 */
    height: calc(100vh - var(--window-bottom) - var(--window-top));
    /* #endif */
    /* #ifndef H5 */
    height: 100vh;
    /* #endif */
    transition: opacity 0.3s;
    background: #999;
    opacity: 0;
    &.active {
        opacity: 1;
    }
    .logo {
        position: relative;
        margin-top: -400upx;
        width: 200upx;
        height: 200upx;
        // z-index: -1;
        opacity: 0;
        transition: opacity 0.3s;
        &.active {
            opacity: 1;
        }
    }
}
.tabbar-box-wrap {
    width: 100%;
    padding: 50upx;
    box-sizing: border-box;
    .tabbar-box {
        display: flex;
        width: 100%;
        background: #fff;
        border-radius: 20upx;
        padding: 15upx 20upx;
        box-sizing: border-box;
        z-index: 2;
        .tabbar-box-item {
            // position: relative;
            width: 100%;
            z-index: 3;
            margin: 10upx;
            color: $uni-color-subtitle;
            text-align: center;
            font-size: $uni-font-size-base;
            .box-image {
                width: 100%;
                height: $uni-img-size-lg;
            }
        }
    }
}

/deep/ .u-popup__content{
    border-radius: 30rpx 30rpx 0 0;
}
</style>

其中中间按钮我是做一个底部弹窗所以就不要组件了,大家自行根据情况改动,这里我们不需要再通过switchTab来进行跳转,只用currentIndex来切换组件即可,下面的u-popup,是底部弹窗,效果如下

2.接下来我们来改动,tabbar中的代码

  1. 修改page.js

首先我们先将主页面放到page.js的第一个,作为入口文件

补充:到这一步,page.js中的tabbar整个就可以删除了

大功告成,目前这种方式,我还没有遇到什么问题和坑,希望评论区大神可以指点一二,初次分享,不吝赐教,谢谢大家,如果大家喜欢,多点赞评论,这也是我继续创作的强劲动力~

<think>嗯,用户遇到了在UniApp中配置tabBar后,在小程序平台不显示的问题。我需要先回想一下常见的导致tabBar不显示的原因。根据之前的经验,可能有几个方面需要检查。 首先,用户是否正确配置了pages.json中的tabBar项?有时候路径写错或者格式不对会导致不显示。比如,pages数组里的页面路径是否正确,tabBar的list中的pagePath是否对应存在的页面。还有,图标路径是否正确,尤其是使用相对路径时容易出错。这时候应该建议用户检查这些配置项,确保路径正确且图标文件存在。 然后,考虑到用户提到的是小程序平台,可能需要检查平台特有的配置。比如,支付宝小程序可能需要额外的设置,或者某些属性在小程序中不支持。例如,某些tabBar的样式选项可能在不同平台有差异,需要查阅uniapp的文档确认兼容性。此外,引用[1]提到在component文件夹下新增自定义tabBar页面可以解决问题,所以如果用户使用了自定义tabBar,可能需要按照这个方法来处理,避免使用原生tabBar导致的显示问题。 还有可能是代码逻辑覆盖了tabBar的显示。比如,在页面中调用了hideTabBar的API,或者在onShow生命周期里错误地隐藏了tabBar。这时候需要检查页面代码,是否有相关调用,比如my.hideTabBar(),这会导致tabBar被隐藏。引用[1]也提到在tabBar页面调用my.hideTabBar()可能会引起问题,所以需要确保没有这样的代码。 另外,初次加载时的闪屏问题可能和自定义tabBar的初始化有关,引用[2]提到自定义tabBar可能导致页面切换时闪烁,用户可能尝试了自定义实现但未正确处理。如果用户尝试自定义tabBar,可能需要按照文档中的最佳实践,比如使用v-if控制显示,或者在onReady之后加载组件,避免闪屏。 还有,编译问题也不能忽视。有时候开发环境缓存可能导致配置未更新,建议用户重新编译或清理缓存。另外,真机调试时可能和开发工具显示不同,需要确认真机上的表现。 总结下来,解决方案的步骤应该包括检查配置是否正确、处理平台差异、避免代码覆盖、处理自定义tabBar以及清理缓存等。需要分点详细说明,确保用户能逐步排查问题。同时,提供示例代码和注意事项,帮助用户理解如何正确配置。此外,相关问题可以引导用户了解更多优化和深入的问题,比如性能分析或动态加载tabBar。</think>### UniApp小程序tabBar不显示的解决方案 #### 一、基础配置检查 1. **pages.json配置验证** 确保在`pages.json`中已正确声明tabBar页面,标准结构为: ```json { "pages": [ {"path": "pages/index/index"}, {"path": "pages/category/category"} ], "tabBar": { "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "static/tabs/home.png", "selectedIconPath": "static/tabs/home-active.png" } ] } } ``` - 检查`pagePath`与`pages`数组中的路径完全一致 - 验证图标路径是否正确(建议使用绝对路径`/static/...`) 2. **平台兼容性处理** 不同小程序平台对tabBar的支持存在差异: ```json "tabBar": { "custom": true, // 微信/支付宝小程序启用自定义模式 "height": "50px" // 支付宝小程序需要明确高度 } ``` #### 二、自定义tabBar实现方案 当原生tabBar失效时,可采用组件化方案: 1. **创建自定义组件** 在`components/tabbar`目录新建组件文件,通过`v-show`控制显隐: ```vue <template> <view class="custom-tabbar" v-show="showTabBar"> <!-- 自定义布局内容 --> </view> </template> ``` 2. **全局状态管理** 使用Vuex存储tabBar显示状态: ```javascript // store/index.js export default new Vuex.Store({ state: { showTabBar: true }, mutations: { setTabBarVisibility(state, visible) { state.showTabBar = visible } } }) ``` 3. **页面注入逻辑** 在需要隐藏tabBar的页面调用: ```javascript onShow() { this.$store.commit('setTabBarVisibility', false) } ``` #### 三、常见问题排查清单 | 现象 | 检测点 | 解决方案 | |------|--------|----------| | 开发工具显示但真机不显示 | 1. 图标路径大小写<br>2. 页面未预编译 | 1. 统一使用小写文件名<br>2. 配置preloadRule预加载 | | 切换页面时闪屏 | 1. 组件加载时序<br>2. CSS渲染冲突 | 1. 使用`v-if`替代`v-show`<br>2. 添加`z-index: 9999` | | 部分平台不显示 | 1. 平台特有样式限制<br>2. 基础库版本过低 | 1. 添加`!important`强制样式<br>2. 设置minPlatformVersion | #### 四、高级调试技巧 1. **真机远程调试** 通过`adb logcat`查看小程序运行时日志,过滤关键字`tabBar`查找报错信息 2. **骨架屏优化** 在自定义tabBar加载前显示占位骨架: ```vue <skeleton-loader v-if="isLoading" /> <custom-tabbar v-else /> ``` 3. **动态tabBar配置** 通过接口动态获取tabBar配置: ```javascript async created() { const res = await uni.request({url: '/api/tabbar-config'}) this.tabList = res.data } ```
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值