微信小程序 TabBar 紅點提醒完美解決方案
TabBar 紅點提醒,很多小程序都需要這個功能比如聊天小程序,電商小程序等
這時候我們需要進行自定義 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": "接口" }] } }
配置好后你會發(fā)現(xiàn)沒有出現(xiàn) Tabbar 這個是正常的
添加代碼文件
在跟目錄中創(chuàng)建文件夾和組件結構
創(chuàng)建完成后可以看到這樣的界面效果
可以看到這塊是一個自定義組建,如果不能出現(xiàn)該效果,可能是因為代碼基礎調試庫的問題,通常在設置自定義 TabBar 提示 TypeError,這時候需要在 詳情-》本地設置-》修改基礎調試庫,不要使用灰度測試版本,我這里實用的是 3.4.2 版本沒有問題
實用 Vant 組建 TabBar
引用
"usingComponents": { "van-tabbar": "@vant/weapp/tabbar/index", "van-tabbar-item": "@vant/weapp/tabbar-item/index" }
設置 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">標簽</van-tabbar-item> <van-tabbar-item icon="setting-o">標簽</van-tabbar-item> </van-tabbar>
設置 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 文件
// 在這個 js 文件中專門創(chuàng)建 store 對象 import {observable,action} from 'mobx-miniprogram' export const store = observable({ numA:1, numB:2, info:3, //計算屬性 get sum(){ return this.numA+this.numB }, //action方法用來修改 store 中的值 updateNum1:action(function(step){ this.numA+=step }), updateNum2:action(function(step){ this.numB+=step }), })
設置 customjs 結構
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 }); } } });
設置 wxml 結構
<!--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">標簽</van-tabbar-item> <van-tabbar-item icon="setting-o">標簽</van-tabbar-item> </van-tabbar>
這時候就可以進行加載
到此這篇關于微信小程序 TabBar 紅點提醒解決方案的文章就介紹到這了,更多相關微信小程序 TabBar 紅點內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
js中document.write和document.writeln的區(qū)別
這篇文章主要介紹了js中document.write和document.writeln的區(qū)別,需要的朋友可以參考下2018-03-03JavaScript實現(xiàn)相冊彈窗功能(zepto.js)
這篇文章主要介紹了JavaScript基于zepto.js實現(xiàn)相冊彈窗功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06JS Generator函數(shù)yield表達式示例詳解
這篇文章主要為大家介紹了JS Generator函數(shù)yield表達式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10