您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何實(shí)現(xiàn)微信小程序自定義底部導(dǎo)航欄組件”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
具體內(nèi)容如下
1、在自己項(xiàng)目的公共組件的文件價(jià)下新建tabbar.vue(定義的自定義導(dǎo)航欄組件)
<template> <view v-if="showTabbar" class="tabbar"> <view v-for="(item, index) in tabList" :key="index" class="icon" @click="switchTabBar(item.path, index)" > <image :src="index == current ? item.iconActivePath : item.iconPath"></image> <text :class="index == current ? 'active_text' : 'text'" bindtap = 'go'>{{ item.name }}</text> </view> </view> </template> <script> // import container from '@/channelMessage/get-container' export default { props: { showTabbar: { type: Boolean, default: true, }, current:{ // 當(dāng)前頁面index type:Number, default:0 }, }, data() { return { selectedIndex: 0, tabList: [ { name: "首頁", iconPath: require("../../../static/image/img/tab-home-nor.png"), iconActivePath: require("../../../static/image/img/tab-home-sel.png"), path: "/pages/index/index", }, { name: "購物車", iconPath: require("../../../static/image/img/tab-cart-nor.png"), iconActivePath: require("../../../static/image/img/tab-cart-sel.png"), path: "/pages/cart/cartEdit", }, { name: "我的", iconPath: require("../../../static/image/img/tab-my-nor.png"), iconActivePath: require("../../../static/image/img/tab-my-sel.png"), path: "/pages/mine/mine", }, ], } }, onShow() { // const containerId = container.getContainerId() // if (containerId == '1000') { // this.showTabbar = false // } }, methods: { switchTabBar(path, index) { this.item_index = index wx.switchTab({ url: path, }) // this.$router.replace(path) }, }, } </script> <style lang="scss" scoped> .tabbar { position: fixed; bottom: 0; z-index: 10; display: flex; align-items: center; justify-content: space-around; width: 100%; height: 100rpx; background-color: #ffffff; padding-bottom: constant(safe-area-inset-bottom); padding-bottom: env(safe-area-inset-bottom); .icon { display: flex; flex-direction: column; align-items: center; image { width: 50rpx; height: 50rpx; } } .active_text{ font-size: 20rpx; margin-top: 5rpx; color: #d81e06; } .text{ font-size: 20rpx; margin-top: 5rpx; } } </style>
2、在項(xiàng)目中的pages.json文件中新增代碼,保證tabbar.vue中的wx.switchTab可以正常使用,代碼如下:
"tabBar": { "selectedColor": "#EE2F51", "list": [{ "pagePath": "pages/index/index", "text": "首頁", "iconPath": "static/image/img/tab-home-nor.png", "selectedIconPath": "static/image/img/tab-home-sel.png" },{ "pagePath": "pages/cart/cartEdit", "text": "購物車", "iconPath": "static/image/img/tab-cart-nor.png", "selectedIconPath": "static/image/img/tab-cart-sel.png" },{ "pagePath": "pages/mine/mine", "text": "我的", "iconPath": "static/image/img/tab-my-nor.png", "selectedIconPath": "static/image/img/tab-my-sel.png" }] },
3、在main.js中全局注冊(cè)自定義組件
import tabBar from "./customComponents/commonComponents/tabBar/index.vue"; //換自己的組件位置,這里的index.vue就是前面所說的tabbar.vue Vue.component("tabBar", tabBar);
4、在需要使用導(dǎo)航欄的頁面引入注冊(cè)的組件
//為頁面引入導(dǎo)航欄組件 <tabBar :current=item_index></tabBar> //標(biāo)記狀態(tài),是的導(dǎo)航欄可以根據(jù)頁面顯示不同的激活狀態(tài) data() { return { item_index: 0, } } //隱藏微信自帶的導(dǎo)航欄 onLoad() { wx.hideTabBar(); },
“如何實(shí)現(xiàn)微信小程序自定義底部導(dǎo)航欄組件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。