Vant
tabbar使用教程:https://youzan.github.io/vant/#/zh-CN/tabbar
關於基本的安裝、引入組件就不再詳細講解,請自行按照文檔安裝章節進行。
封裝Tabbar
在不同頁面顯示tabbar,如果tabbar修改,則需要變動多個頁面
封裝成組件后,統一引用組件,修改tabbar則只需要改動組件文件
封裝后引用代碼
active代表要高亮組件中第幾個圖標,必須為數值
<template> <!-- 這里顯示其他內容 --> <TabbarHtml v-bind:active=2 /> </template> <script> import TabbarHtml from '@/components/Tabbar.vue' export default { components: { TabbarHtml } } </script>
tabbar組件代碼
- 使用tabbarTempValue值來監聽,使用active值來接收。這是為了防止props為單向數據綁定,在組件內改變值后會產生報錯,父頁面無法接收
- onChange事件監聽並路由跳轉 這里使用的是vue-router
<template> <div class="tabbar"> <van-tabbar v-model="tabbarTempValue" @change="onChange"> <van-tabbar-item icon="home-o" url="/Home">標簽</van-tabbar-item> <van-tabbar-item icon="search">標簽</van-tabbar-item> <van-tabbar-item icon="friends-o">標簽</van-tabbar-item> <van-tabbar-item icon="setting-o">標簽</van-tabbar-item> </van-tabbar> </div> </template> <script> import Vue from 'vue'; import { Tabbar, TabbarItem } from 'vant'; import { Icon } from 'vant'; import { Notify } from 'vant'; Vue.use(Notify); Vue.use(Tabbar).use(TabbarItem); Vue.use(Icon); export default { props: { active: Number }, data() { return { tabbarTempValue: this.active } }, methods: { onChange(index) { const routerArray = [ "/", "/Home", "/Home", "/Home" ]; this.$router.push(routerArray[index]) } } } </script>
轉載 :https://cloud.tencent.com/developer/article/1582782
