最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue封裝一個(gè)Tabbar組件?帶組件路由跳轉(zhuǎn)方式

 更新時(shí)間:2022年04月28日 09:21:47   作者:外下羊.  
這篇文章主要介紹了Vue封裝一個(gè)Tabbar組件?帶組件路由跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vue封裝Tabbar組件

話不多說直接上代碼

在App.vue 封裝 路由跳轉(zhuǎn) 利用router-view的特性

<template>
? <div id="app">
? ? <router-view />
? ? //引入子組件 把items 這個(gè)數(shù)組傳進(jìn)去
? ? <Tabr :items="items" />
? </div>
</template>
<script>
import Tabr from "./components/Tabr";
export default {
? components: {
? ? Tabr,
? },
? data() {
? ? return {
? ? ? items: [
? ? ? ? {
? ? ? ? ? title: "首頁",
? ? ? ? ? path: "/",
? ? ? ? ? img: require("./assets/img/shouye.png"),
? ? ? ? ? imgs: require("./assets/img/shouye_1.png"),
? ? ? ? },
? ? ? ? {
? ? ? ? ? title: "分類",
? ? ? ? ? path: "/About",
? ? ? ? ? img: require("./assets/img/fenlei.png"),
? ? ? ? ? imgs: require("./assets/img/fenlei_1.png"),
? ? ? ? },
? ? ? ? {
? ? ? ? ? title: "購物車",
? ? ? ? ? path: "/Cart",
? ? ? ? ? img: require("./assets/img/gouwuchezhengpin.png"),
? ? ? ? ? imgs: require("./assets/img/gouwuchezhengpin_1.png"),
? ? ? ? },
? ? ? ? {
? ? ? ? ? title: "我的",
? ? ? ? ? path: "/Mime",
? ? ? ? ? img: require("./assets/img/wode.png"),
? ? ? ? ? imgs: require("./assets/img/wode_1.png"),
? ? ? ? },
? ? ? ],
? ? };
? },
};
</script>

子組件Tabbar

<template>
? <div class="Yanxuan_tab">
? ? <div v-for="(item,index) in items" :key="index" @click="Onclick(index)">
? ? ? <div>
? ? ? ? <img :src="index===Tabindex?item.imgs:item.img" alt ?/>
? ? ? ? //動(dòng)態(tài)綁定
? ? ? </div>
? ? ? <div :class="index===Tabindex?'title':'Yanxuan_title'">
? ? ? //
? ? ? {{item.title}}</div>
? ? </div>
? </div>
</template>
<script>
export default {
? props: {
? ? items: {
? ? ? type: Array,
? ? ? required: true,
? ? ? validator: function (value) {
? ? ? ? return value.length <= 6;
? ? ? ? //市面常見的Tabbar的欄 不能超過6個(gè)
? ? ? },
? ? }
? },
? data() {
? ? ? return {
? ? ? ? ? item:this.items,
? ? ? ? ? Tabindex:0
? ? ? }
? },
? methods:{
? ? ?Onclick(index){
? ? ?//這里是根據(jù)下標(biāo)切換 圖片樣式跟字體顏色 動(dòng)態(tài)綁定
? ? ? ?this.Tabindex = index
? ? ? ?var temp = this.item[index]
? ? ? this.$router.push(temp.path)
? ? ?}
? }
};
</script>
<style scoped>
.Yanxuan_tab {
? width: 100%;
? height: 64px;
? border: 1px solid gainsboro;
? position: fixed;
? bottom: 0px;
? left: 0px;
? display: inline-flex;
? justify-content: space-around;
? align-items: center;
? text-align: center;
?
}
.Yanxuan_title{
? ? ?font-size: 14px;
}
.title{
? ? font-size: 14px;
? ? color:red
}
</style>

然后就是配置的路由

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
  const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta:{
      isShowTabbar:true
    }
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
    meta:{
      isShowTabbar:true
    }
  }
  ,
  {
    path: '/Cart',
    name: 'Cart',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Cart.vue'),
    meta:{
      isShowTabbar:false
    }
  },
  {
    path: '/Mime',
    name: 'Mime',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Mime.vue'),
    meta:{
      isShowTabbar:true
    }
  },
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
export default router

最后的效果完成圖

代碼就直接可以用了

從零開始封裝一個(gè)Tabbar

首先底部菜單欄最外層是一個(gè)div,

在div中整體上存在四個(gè)或五個(gè)小div,每個(gè)div中包含icon和text,如下圖

         

對(duì)于每一個(gè)icon對(duì)象,它包含圖標(biāo)以及文字,但十實(shí)際中我們肯定不會(huì)將img和text寫死,以及處于active狀態(tài)時(shí)text的顏色也不會(huì)寫死,以方便調(diào)用者傳入想要的參數(shù),這樣才算一個(gè)徹底的封裝。

代碼如下

<template>
? <div class="tab-bar-item" @click="itemClick">
? ? <div v-if="!isActive">
? ? ? <slot name="item-icon"></slot>
? ? </div>
? ? <div v-else>
? ? ? <slot name="item-icon-active"></slot>
? ? </div>
? ? <div :style="activeStyle">
? ? ? <slot name="item-text"></slot>
? ? </div>
? ??
? </div>
</template>
?
<!--
? ? 上方代碼設(shè)置三個(gè)插槽,為什么不是兩個(gè)呢,因?yàn)檫€要包含當(dāng)item處于活躍狀態(tài)時(shí)要顯示的image,所以是三個(gè),使用v-if控制當(dāng)非活躍時(shí)顯示默認(rèn)icon插槽,活躍時(shí)顯示亮色icon插槽。因?yàn)椴宀凼且粋魅肫渲械膬?nèi)容覆蓋的,所以傳入的內(nèi)容可能會(huì)將我們slot中的一些屬性覆蓋掉,所以常常我們需要將slot包裹在div中,這樣就可以避免這個(gè)問題。
? ? icon下方文同理也放在div中,style綁定一個(gè)計(jì)算屬性,看下方代碼可以這個(gè)計(jì)算屬性當(dāng)item處于活躍時(shí)會(huì)返回顏色屬性,當(dāng)然這個(gè)屬性也是可以在調(diào)用tab-bar時(shí)傳入的,默認(rèn)為紅色。
-->
<script>
? export default {
? ? name:'TabBarItem',
? ? props:{
? ? ? path: String, ?// 當(dāng)前item對(duì)應(yīng)的路由,由調(diào)用者指定
? ? ? activeColor:{ ?// 當(dāng)前item的文字在活躍時(shí)的顏色,默認(rèn)紅色,可由使用者指定
? ? ? ? type:String,
? ? ? ? default:"red"
? ? ? }
? ? },
? ? data() {
? ? ? return {
? ? ? ? // isActive:false,
? ? ? }
? ? },
? ? computed:{
? ? ? // 判斷當(dāng)前item是否處于活躍狀態(tài)
? ? ? isActive(){
? ? ? ? return this.$route.path.indexOf(this.path)!==-1;
? ? ? },
? ? ? // 計(jì)算屬性,如果處于活躍狀態(tài)則設(shè)置style,否則去除style
? ? ? activeStyle(){
? ? ? ? return this.isActive? {color:this.activeColor}:{};
? ? ? }
? ? },
? ? methods:{
? ? ? itemClick(){
? ? ? ? if(this.$route.path!==this.path){
? ? ? ? ? this.$router.push(this.path);
? ? ? ? ? // this.isActive = true;
? ? ? ? }?
? ? ? }
? ? }
? }
</script>
<style scoped>
<!--一些默認(rèn)樣式-->
? .tab-bar-item {
? ? flex: 1;
? ? text-align: center;
? ? height: 49px;
? ? font-size: 10px;
? }
? .tab-bar-item img {
? ? margin-top: 4px;
? ? width: 22px;
? ? height: 22px;
? ? vertical-align: middle;
? ? margin-bottom: 2px;
? }
</style>

封裝完每一個(gè)tabbaritem后

接下來是整體的tabbar,試想,我們肯定還是放入一個(gè)插槽代碼如下: 

<template>
? <div id="tab-bar">
? ? <slot></slot>
? </div>
</template>
<script>
export default {
? name: "TabBar"
};
</script>
<style scoped>
#tab-bar {
? display: flex;
? background-color: #f6f6f6;
? position: fixed;
? left: 0;
? right: 0;
? bottom: 0;
? box-shadow: 0px -2px 1px rgba(100, 100, 100, 0.1);
}
?
</style>

tabbar預(yù)留的插槽則用于放入每一個(gè)item,我們?cè)谶@里也是不能寫死的,因?yàn)榭丶_發(fā)者并不知需要放入多少個(gè)item。

使用者在使用我們封裝的控件時(shí)

則可以如下代碼,放入內(nèi)容:

<template>
? <tab-bar>
? ? ? <tab-bar-item path="/home" activeColor="deepPink">
? ? ? ? <img slot="item-icon" src="~assets/img/tabbar/home.svg" alt="">
? ? ? ? <img slot="item-icon-active" src="~assets/img/tabbar/home_active.svg" alt="">
? ? ? ? <div slot="item-text">首頁</div>
? ? ? </tab-bar-item>
? ? ? <tab-bar-item path="/category" activeColor="deepPink">
? ? ? ? <img slot="item-icon" src="~assets/img/tabbar/category.svg" alt="">
? ? ? ? <img slot="item-icon-active" src="~assets/img/tabbar/category_active.svg" alt="">
? ? ? ? <div slot="item-text">分類</div>
? ? ? </tab-bar-item>
? ? ? <tab-bar-item path="/cart" activeColor="deepPink">
? ? ? ? <img slot="item-icon" src="~assets/img/tabbar/cart.svg" alt="">
? ? ? ? <img slot="item-icon-active" src="~assets/img/tabbar/cart_active.svg" alt="">
? ? ? ? <div slot="item-text">購物車</div>
? ? ? </tab-bar-item>
? ? ? <tab-bar-item path="/profile" activeColor="deepPink">
? ? ? ? <img slot="item-icon" src="~assets/img/tabbar/profile.svg" alt="">
? ? ? ? <img slot="item-icon-active" src="~assets/img/tabbar/profile_active.svg" alt="">
? ? ? ? <div slot="item-text">我的</div>
? ? ? </tab-bar-item>
? ? </tab-bar>
??
</template>
<script>
? import TabBar from "components/tabbar/TabBar";
? import TabBarItem from "components/tabbar/TabBarItem";
? export default {
? ? name:'MainTabBar',
? ? components:{
? ? ? TabBar,
? ? ? TabBarItem
? ? }
? }
</script>
<style scoped>
</style>

到此結(jié)束。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue使用混入定義全局變量、函數(shù)、篩選器的實(shí)例代碼

    vue使用混入定義全局變量、函數(shù)、篩選器的實(shí)例代碼

    本文主要是給大家分享利用混入mixins來實(shí)現(xiàn)全局變量和函數(shù)。這種方法優(yōu)點(diǎn)是ide會(huì)有方法、變量、篩選器提示。對(duì)vue中 利用混入定義全局變量、函數(shù)、篩選器的相關(guān)知識(shí)感興趣的朋友,跟隨小編一起看看吧
    2019-07-07
  • 關(guān)于Vue單頁面骨架屏實(shí)踐記錄

    關(guān)于Vue單頁面骨架屏實(shí)踐記錄

    這篇文章主要給大家介紹了關(guān)于Vue單頁面骨架屏實(shí)踐的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-12-12
  • vue+element?tree懶加載更新數(shù)據(jù)的示例代碼

    vue+element?tree懶加載更新數(shù)據(jù)的示例代碼

    這篇文章主要介紹了vue+element?tree懶加載更新數(shù)據(jù),文中給大家補(bǔ)充介紹了Vue Element Ui 樹形表懶加載新增、修改、刪除等操作后局部數(shù)據(jù)更新的詳細(xì)代碼,感興趣的朋友跟隨小編一起看看吧
    2022-09-09
  • Vue首屏加載過慢的優(yōu)化方法

    Vue首屏加載過慢的優(yōu)化方法

    Vue 應(yīng)用在首屏加載時(shí)速度過慢,通常與以下問題有關(guān):打包文件過大、網(wǎng)絡(luò)請(qǐng)求過多、資源加載過慢,針對(duì)這些問題,本文給大家介紹了Vue首屏加載過慢的優(yōu)化方法,需要的朋友可以參考下
    2025-01-01
  • vue3中vuex與pinia的踩坑筆記記錄

    vue3中vuex與pinia的踩坑筆記記錄

    Vuex是一個(gè)專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,它采用集中式存儲(chǔ)管理應(yīng)用的所有組件的狀態(tài),并以相應(yīng)的規(guī)則保證狀態(tài)以一種可預(yù)測的方式發(fā)生變化,這篇文章主要給大家介紹了關(guān)于vue3中vuex與pinia踩坑的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • 淺談element中InfiniteScroll按需引入的一點(diǎn)注意事項(xiàng)

    淺談element中InfiniteScroll按需引入的一點(diǎn)注意事項(xiàng)

    這篇文章主要介紹了淺談element中InfiniteScroll按需引入的一點(diǎn)注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Vue--keep-alive使用實(shí)例詳解

    Vue--keep-alive使用實(shí)例詳解

    這篇文章主要介紹了Vue--keep-alive使用實(shí)例詳解,keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染,用示例代碼介紹Vue的keep-alive的用法,需要的朋友可以參考下
    2022-08-08
  • Vue.js 事件修飾符的使用教程

    Vue.js 事件修飾符的使用教程

    在實(shí)際開發(fā)中,不可避免的會(huì)使用到對(duì)于事件的操作,如何處理 DOM 事件流,成為我們必須要掌握的技能。這篇文章主要介紹了Vue.js 事件修飾符的使用教程,需要的朋友可以參考下
    2018-11-11
  • Vue組件通信之父傳子與子傳父深入探究

    Vue組件通信之父傳子與子傳父深入探究

    對(duì)于vue來說,組件之間的消息傳遞是非常重要的,用vue可以是要組件復(fù)用的,而組件實(shí)例的作用域是相互獨(dú)立,這意味著不同組件之間的數(shù)據(jù)無法互相引用,一般來說,組件之間可以有幾種關(guān)系,下面是我對(duì)組件之間消息傳遞的常用方式的總結(jié)
    2022-12-12
  • Vue-CLI項(xiàng)目中路由傳參的方式詳解

    Vue-CLI項(xiàng)目中路由傳參的方式詳解

    這篇文章主要介紹了Vue-CLI項(xiàng)目中路由傳參的方式詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

安西县| 安乡县| 精河县| 余姚市| 慈溪市| 磴口县| 太保市| 遂宁市| 山东省| 建平县| 湟源县| 罗源县| 新密市| 苏尼特左旗| 密山市| 沽源县| 临桂县| 泰和县| 白山市| 酒泉市| 都昌县| 巴马| 阳西县| 南漳县| 青冈县| 都匀市| 阳曲县| 万载县| 桐梓县| 门源| 芜湖县| 华池县| 修水县| 南岸区| 永泰县| 丰都县| 克拉玛依市| 屏南县| 临朐县| 新津县| 隆安县|