欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Vue封裝Tabbar組件

話不多說直接上代碼

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

<template>
? <div id="app">
? ? <router-view />
? ? //引入子組件 把items 這個數(shù)組傳進去
? ? <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 ?/>
? ? ? ? //動態(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個
? ? ? },
? ? }
? },
? data() {
? ? ? return {
? ? ? ? ? item:this.items,
? ? ? ? ? Tabindex:0
? ? ? }
? },
? methods:{
? ? ?Onclick(index){
? ? ?//這里是根據(jù)下標(biāo)切換 圖片樣式跟字體顏色 動態(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

最后的效果完成圖

代碼就直接可以用了

從零開始封裝一個Tabbar

首先底部菜單欄最外層是一個div,

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

         

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

代碼如下

<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è)置三個插槽,為什么不是兩個呢,因為還要包含當(dāng)item處于活躍狀態(tài)時要顯示的image,所以是三個,使用v-if控制當(dāng)非活躍時顯示默認icon插槽,活躍時顯示亮色icon插槽。因為插槽是要被傳入其中的內(nèi)容覆蓋的,所以傳入的內(nèi)容可能會將我們slot中的一些屬性覆蓋掉,所以常常我們需要將slot包裹在div中,這樣就可以避免這個問題。
? ? icon下方文同理也放在div中,style綁定一個計算屬性,看下方代碼可以這個計算屬性當(dāng)item處于活躍時會返回顏色屬性,當(dāng)然這個屬性也是可以在調(diào)用tab-bar時傳入的,默認為紅色。
-->
<script>
? export default {
? ? name:'TabBarItem',
? ? props:{
? ? ? path: String, ?// 當(dāng)前item對應(yīng)的路由,由調(diào)用者指定
? ? ? activeColor:{ ?// 當(dāng)前item的文字在活躍時的顏色,默認紅色,可由使用者指定
? ? ? ? type:String,
? ? ? ? default:"red"
? ? ? }
? ? },
? ? data() {
? ? ? return {
? ? ? ? // isActive:false,
? ? ? }
? ? },
? ? computed:{
? ? ? // 判斷當(dāng)前item是否處于活躍狀態(tài)
? ? ? isActive(){
? ? ? ? return this.$route.path.indexOf(this.path)!==-1;
? ? ? },
? ? ? // 計算屬性,如果處于活躍狀態(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>
<!--一些默認樣式-->
? .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>

封裝完每一個tabbaritem后

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

<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ù)留的插槽則用于放入每一個item,我們在這里也是不能寫死的,因為控件開發(fā)者并不知需要放入多少個item。

使用者在使用我們封裝的控件時

則可以如下代碼,放入內(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é)束。

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

相關(guān)文章

  • vue-devtools的安裝和使用步驟詳解

    vue-devtools的安裝和使用步驟詳解

    在本篇文章中小編給大家整理的是一篇關(guān)于vue-devtools安裝使用的相關(guān)知識點內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-10-10
  • vue基本使用--refs獲取組件或元素的實例

    vue基本使用--refs獲取組件或元素的實例

    今天小編就為大家分享一篇vue基本使用--refs獲取組件或元素的實例,具有很好的參考價值,希望對大家有所幫助。一起個跟隨小編過來看看吧
    2019-11-11
  • vue中created、watch和computed的執(zhí)行順序詳解

    vue中created、watch和computed的執(zhí)行順序詳解

    由于vue的雙向數(shù)據(jù)綁定,自動更新數(shù)據(jù)的機制,在數(shù)據(jù)變化后,對此數(shù)據(jù)依賴?的所有數(shù)據(jù),watch事件都會被更新、觸發(fā),下面這篇文章主要給大家介紹了關(guān)于vue中created、watch和computed的執(zhí)行順序,需要的朋友可以參考下
    2022-11-11
  • vue.js 實現(xiàn)評價五角星組件的實例代碼

    vue.js 實現(xiàn)評價五角星組件的實例代碼

    這篇文章主要介紹了vue.js 實現(xiàn)評價五角星組件的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • 使用vue自定義指令開發(fā)表單驗證插件validate.js

    使用vue自定義指令開發(fā)表單驗證插件validate.js

    今天就來介紹一下如何利用vue的自定義指令directive來開發(fā)一個表單驗證插件的過程,需要的朋友可以參考下
    2019-05-05
  • vue3 vite配置跨域及不生效問題解決

    vue3 vite配置跨域及不生效問題解決

    這篇文章主要介紹了vue3 vite配置跨域以及不生效問題,本文給大家分享完美解決方案,需要的朋友可以參考下
    2023-07-07
  • vue實現(xiàn)登錄功能

    vue實現(xiàn)登錄功能

    這篇文章主要介紹了vue實現(xiàn)登錄功能的步驟,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • vue中table表頭單元格合并(附單行、多級表頭代碼)

    vue中table表頭單元格合并(附單行、多級表頭代碼)

    本文主要介紹了vue中table表頭單元格合并(附單行、多級表頭代碼),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • vue中使用iconfont圖標(biāo)的過程

    vue中使用iconfont圖標(biāo)的過程

    這篇文章主要介紹了vue中使用iconfont圖標(biāo)的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Vue 換膚的示例實踐

    Vue 換膚的示例實踐

    本篇文章主要介紹了Vue 換膚的示例實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論