微信小程序 TabBar 紅點(diǎn)提醒完美解決方案
TabBar 紅點(diǎn)提醒,很多小程序都需要這個(gè)功能比如聊天小程序,電商小程序等
這時(shí)候我們需要進(jìn)行自定義 TabBar
配置信息
更改 custom 為 True 變?yōu)樽远x Tabbar
{
"pages": [
"pages/home/home",
"pages/index/index",
"pages/logs/logs"
],
"window": {
"navigationBarTextStyle": "black",
"navigationBarTitleText": "Weixin",
"navigationBarBackgroundColor": "#ffffff"
},
"componentFramework": "glass-easel",
"sitemapLocation": "sitemap.json",
"lazyCodeLoading": "requiredComponents",
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"my-numbers": "./components/numbers/numbers"
},
"tabBar": {
"custom": true,
"color": "#000000",
"selectedColor": "#000000",
"backgroundColor": "#ffffff",
"list": [{
"pagePath": "pages/home/home",
"text": "組件"
}, {
"pagePath": "pages/index/index",
"text": "接口"
}]
}
}配置好后你會(huì)發(fā)現(xiàn)沒有出現(xiàn) Tabbar 這個(gè)是正常的
添加代碼文件
在跟目錄中創(chuàng)建文件夾和組件結(jié)構(gòu)

創(chuàng)建完成后可以看到這樣的界面效果

可以看到這塊是一個(gè)自定義組建,如果不能出現(xiàn)該效果,可能是因?yàn)榇a基礎(chǔ)調(diào)試庫的問題,通常在設(shè)置自定義 TabBar 提示 TypeError,這時(shí)候需要在 詳情-》本地設(shè)置-》修改基礎(chǔ)調(diào)試庫,不要使用灰度測試版本,我這里實(shí)用的是 3.4.2 版本沒有問題
實(shí)用 Vant 組建 TabBar
引用
"usingComponents": {
"van-tabbar": "@vant/weapp/tabbar/index",
"van-tabbar-item": "@vant/weapp/tabbar-item/index"
}設(shè)置 Tabbar 樣式
<!--custom-tab-bar/index.wxml-->
<van-tabbar active="{{ active }}" bind:change="onChange">
<van-tabbar-item info="3">
<image
slot="icon"
src="{{ icon.normal }}"
mode="aspectFit"
style="width: 30px; height: 18px;"
/>
<image
slot="icon-active"
src="{{ icon.active }}"
mode="aspectFit"
style="width: 30px; height: 18px;"
/>
自定義
</van-tabbar-item>
<van-tabbar-item icon="search">標(biāo)簽</van-tabbar-item>
<van-tabbar-item icon="setting-o">標(biāo)簽</van-tabbar-item>
</van-tabbar>
設(shè)置 JS
// custom-tab-bar/index.js
Component({
/**
* 組件的屬性列表
*/
properties: {
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
active: 0,
icon: {
normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
active: 'https://img.yzcdn.cn/vant/user-active.png',
},
},
/**
* 組件的方法列表
*/
methods: {
onChange(event) {
this.setData({ active: event.detail });
}
}
})共享數(shù)據(jù)給 info 屬性
創(chuàng)建 store 文件夾-》創(chuàng)建 storejs 文件
// 在這個(gè) js 文件中專門創(chuàng)建 store 對象
import {observable,action} from 'mobx-miniprogram'
export const store = observable({
numA:1,
numB:2,
info:3,
//計(jì)算屬性
get sum(){
return this.numA+this.numB
},
//action方法用來修改 store 中的值
updateNum1:action(function(step){
this.numA+=step
}),
updateNum2:action(function(step){
this.numB+=step
}),
})設(shè)置 customjs 結(jié)構(gòu)
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings';
import { store } from '../store/store';
Component({
behaviors: [storeBindingsBehavior],
properties: {},
storeBindings: {
store,
fields: {
numA: () => store.numA,
numB: () => store.numB,
sum: 'sum'
},
actions: {
buttonTap: 'update'
}
},
data: {
info: 0,
active: 0,
icon: {
normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
active: 'https://img.yzcdn.cn/vant/user-active.png',
}
},
observers: {
'sum': function(val) {
this.setData({
info: val
});
}
},
methods: {
myMethod() {
this.setData({
info: this.data.sum
});
}
}
});設(shè)置 wxml 結(jié)構(gòu)
<!--custom-tab-bar/index.wxml-->
<van-tabbar active="{{ active }}" bind:change="onChange">
<van-tabbar-item info="{{numA}}">
<image
slot="icon"
src="{{ icon.normal }}"
mode="aspectFit"
style="width: 30px; height: 18px;"
/>
<image
slot="icon-active"
src="{{ icon.active }}"
mode="aspectFit"
style="width: 30px; height: 18px;"
/>
自定義
</van-tabbar-item>
<van-tabbar-item icon="search">標(biāo)簽</van-tabbar-item>
<van-tabbar-item icon="setting-o">標(biāo)簽</van-tabbar-item>
</van-tabbar>這時(shí)候就可以進(jìn)行加載

到此這篇關(guān)于微信小程序 TabBar 紅點(diǎn)提醒解決方案的文章就介紹到這了,更多相關(guān)微信小程序 TabBar 紅點(diǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例
這篇文章主要為大家介紹了從JavaScript純函數(shù)解析最深刻的函子 Monad實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
基于JavaScript實(shí)現(xiàn)添加到購物車效果附源碼下載
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)添加到購物車效果附源碼下載的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
JavaScript實(shí)現(xiàn)QQ列表展開收縮擴(kuò)展功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)QQ列表展開收縮擴(kuò)展功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10

