PS后續:
說來慚愧, 沒想到這篇文章瀏覽的人有點多. 說實話寫的挺亂的. 並且自定義tabbar還有閃屏的問題.
因為有好幾位道友都問了這個問題, 其中一位因為項目很急,所以就研究了一下(也是借鑒大佬的想法), 差不多解決了閃屏的問題.但還是有點小瑕疵.
解決自定義tabbar閃屏的問題, 參考鏈接: https://developers.weixin.qq.com/community/develop/doc/000c6e038c0ce05413f71e7ce56c04
本人也是前端路上的小白(工作滿打滿算才 一年半...), 所以寫的有什么不對的地方, 還望指出吶.
解決閃屏的代碼就直接放壓縮包鏈接了, 需要的話可以下載去借鑒 : https://files.cnblogs.com/files/yk95/wepy-poster-test.zip
運行項目之前, 還是去看一下大佬的文章, 這樣思路更清晰.
運行項目, 需要對wepy有所了解, 不了解的可以去查看官方文檔,
還有小程序項目的appId需要弄成測試號, 不然這是我的(因為懶,就沒刪appId),沒法跑.
還有, 項目的初始頁面得是自定義tabbar頁面的其中一個, 也就是在app.json中config下的pages, 不然的話會出問題(否則只能在每個頁面去隱藏官方tabbar了) -- 先看參考鏈接, 先看參考鏈接, 先看參考鏈接,
對於下面的文章, 因為寫的不好, 並且問題也多, 所以不建議去看了. 可以直接下載代碼壓縮包,畢竟在編譯器上看代碼還是最爽的.
因為需求,小程序自帶的tabBar不能滿足, 所以只能來自己重新寫一個. 先看效果圖吧
首頁:

發現:

消息:

我的:

接下來看代碼:
1- 組件-- tabBarBottom.wpy 這里頁面也可以用循環來寫, 不過就要在樣式上再去調整, 我這里沒有用循環, 就將就看吧.....
view 中的 c-y 與 c-gray 是公共樣式中, 控制圖標顏色的切換; 因為這里的圖標我用的是阿里雲圖標, 不是圖片, 可以自己替換成圖片, 根據 selected 進行圖片切換
<template>
<view class="tabBarBox">
<!-- 首頁 -->
<navigator class="itemView" url="{{tabBar.list[0].pagePath}}" open-type="redirect" hover-class="none">
<view class="item_icon {{tabBar.list[0].selected ? 'c-y' : 'c-gray'}} {{tabBar.list[0].icon_class}}"></view>
//如果替換成圖片 寫法 替換圖片注意樣式, 樣式應該要進行調整
//<image class="" src="{{tabBar.list[0].selected ? 'tabBar.list[0].img_act' : 'tabBar.list[0].img'}}">
<view class="item_text {{tabBar.list[0].selected ? 'c-y' : 'c-gray'}}">{{tabBar.list[0].text}}</view>
</navigator>
<!-- 發現 -->
<navigator class="itemView" url="{{tabBar.list[1].pagePath}}" open-type="redirect" hover-class="none">
<view class="item_icon {{tabBar.list[1].selected ? 'c-y' : 'c-gray'}} {{tabBar.list[1].icon_class}}"></view>
<view class="item_text {{tabBar.list[1].selected ? 'c-y' : 'c-gray'}}">{{tabBar.list[1].text}}</view>
</navigator>
<!-- 發布 -->
<view class="addView">
<image class="add_icon" src="../images/add.png"></image>
</view>
<!-- 消息 -->
<navigator class="itemView2 itemView" url="{{tabBar.list[2].pagePath}}" open-type="redirect" hover-class="none">
<view class="item_icon {{tabBar.list[2].selected ? 'c-y' : 'c-gray'}} {{tabBar.list[2].icon_class}}"></view>
<view class="item_text {{tabBar.list[2].selected ? 'c-y' : 'c-gray'}}">{{tabBar.list[2].text}}</view>
</navigator>
<!-- 我的 -->
<navigator class="itemView" url="{{tabBar.list[3].pagePath}}" open-type="redirect" hover-class="none">
<view class="item_icon {{tabBar.list[3].selected ? 'c-y' : 'c-gray'}} {{tabBar.list[3].icon_class}}"></view>
<view class="item_text {{tabBar.list[3].selected ? 'c-y' : 'c-gray'}}">{{tabBar.list[3].text}}</view>
</navigator>
<!-- <view></view> -->
</view>
</template>
<script>
import wepy from 'wepy';
export default class tabBar extends wepy.component {
// props 接收父組件傳遞過來的值
props = {
// 接收父級傳遞的tabBar信息
tabBar: {
type: Object,
default: {}
}
}
components = {
}
data = {
}
onLoad() {
}
computed = {}
methods = {
}
event = {}
}
</script>
<style lang="scss">
.tabBarBox{
width: 100%;
height: 100rpx;
background-color: #fff;
position: fixed;
bottom: 0;
z-index: 9999;
border-top: 1px #afafaf solid;
}
.itemView2{
margin-left: 150rpx;
}
.itemView{
width: 150rpx;
height: 100rpx;
text-align: center;
display: inline-block;
padding-top: 6rpx;
.item_icon{
font-size: 50rpx;
}
.item_text{
font-size: 28rpx;
}
}
.addView{
width: 150rpx;
position: fixed;
bottom: 0;
text-align: center;
display: inline-block;
.add_icon{
width: 120rpx;
height: 120rpx;
}
}
</style>
2- tabBar的數據 , 我放在了啟動文件中 app.wpy
1 globalData = { 2 userInfo: null,
// tabBar數據 3 tabBar:{ 4 list: [ 5 { 6 pagePath: "home", 7 text: "首頁", 8 icon_class: "iconfont icon-tab-home", //這里用的是阿里圖標, 可以替換成圖片 9 selected: true
//圖片寫法
// img: '未選中的圖片路徑',
// img_act: '被選中的圖片路徑'
10 }, 11 { 12 pagePath: "find", 13 text: "發現", 14 icon_class: "iconfont icon-tab-find", 15 selected: false 16 }, 17 { 18 pagePath: "news", 19 text: "消息", 20 icon_class: "iconfont icon-tab-news", 21 selected: false 22 }, 23 { 24 pagePath: "myInfo", 25 text: "我的", 26 icon_class: "iconfont icon-tab-my", 27 selected: false 28 } 29 ] 30 } 31 }
// 處理tabBar中點擊, 被點擊,將當前的數據對象中 selected 改成true, 其余的就得改成 false; 這里的id是標識, 在調用時手動傳入的; id 與 tabBar數組每一個對象索引要對應 32 tabBarClickHandle(id, that) { 33 let tbList = this.globalData.tabBar.list; 34 tbList.forEach((item, index) => { 35 if (id == index) { 36 tbList[id].selected = true; 37 } else { 38 tbList[index].selected = false; 39 } 40 }); 41 that.$apply(); 42 return this.globalData.tabBar; 43 }
3- 首頁中使用組件 剩余的 發現,消息,我的這三個頁面中都是這樣的用法, 都是這五步, 不過剩余的三個 在第四步中 id要變 發現--id-1 消息--id-2 我的--id-3
1 <template> 2 <view id="HomePage"> 3 <view>
// ⑤: 使用組件, 將數據傳遞給組件 4 <tabBarBottom :tabBar.sync="tabBarData"></tabBarBottom> 5 </view> 6 </view> 7 </template> 8 <script> 9 import wepy from 'wepy'; 10 import tabBarBottom from '@/components/tabBarBottom'; //①:先導入組價 11 export default class Home extends wepy.page{ 12 config = { 13 navigationBarTitleText: "首頁 14 } 15 components = { 16 tabBarBottom, // ② 聲明組件 17 } 18 data = { 19 tabBarData: {}, //③ 組件數據 <傳遞給組件的> 20 } 21 onLoad() {
//④: 獲取數據,更新數據 tabBarClickHandle()啟動文件中編寫的---- 0就是形參id的實參
22 this.tabBarData = this.$parent.tabBarClickHandle(0, this); 23 this.$apply(); 24 } 25 computed = { 26 27 } 28 methods = { 29 30 } 31 event = { 32 33 } 34 } 35 </script>
慢慢積累,慢慢成長,加油!!
文章中如果有錯誤,請您指出,我會第一時間去修改;
①:
