亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用Vue封裝導(dǎo)航欄組件

 更新時(shí)間:2021年09月14日 17:16:38   作者:Huangrunze!  
這篇文章主要為大家詳細(xì)介紹了用Vue封裝導(dǎo)航欄組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言:把一個(gè)功能模塊使用組件化的思想充分封裝,如導(dǎo)航欄,這無(wú)論對(duì)我們的開(kāi)發(fā)思想還是效率都有許多好處,在開(kāi)發(fā)中,我們要盡量多得運(yùn)用組件化的開(kāi)發(fā)思想,不要把所有代碼都寫(xiě)在同一個(gè).vue文件中,這樣能大大提高代碼的可讀性。

封裝導(dǎo)航欄

主要思路:把紅色的部分當(dāng)成一個(gè)個(gè)組件,而他們只是圖片和文字不同,所以我們可以把他們封裝成同一個(gè)組件,然后向組件里傳入圖片信息和文字信息即可(可以用插槽)。

//TabBarItem.vue
<template>
  <div class="tabBarItem" @click="tabBarItemClick">
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <div :style="isActiveColor">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name:"TabBarItem",
  props:{
    path:String,
    activeColor:{
      type:String,
      default:"pink"
    }
  },
  computed:{
    isActive:{
      get(){
        return this.$route.path.indexOf(this.path)!==-1;
      },
      set(){}
    },
    isActiveColor(){
      return this.isActive?{color:this.activeColor}:{}
    }
  },
  methods:{
    tabBarItemClick(){
      this.$router.push(this.path);
    }
  }
}
</script>

<style scoped>
.tabBarItem{
  flex: 1;
  font-size: 12px;
}
.tabBarItem img{
  margin-top: 3px;
  width: 24px;
  padding-bottom:3px ;
}
</style>

接下來(lái)就是封裝一個(gè)把這4個(gè)選項(xiàng)放在同一個(gè)地方的容器。

//TabBar.vue
<template>
  <div class="tabBar">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name:"TabBar"
}
</script>

<style scoped>
.tabBar{
  display: flex;
  height: 49px;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  text-align: center;
  box-shadow: 0px -1px 1px rgba(100, 100, 100, .1);
  background-color: #f6f6f6;
}

</style>

再接下來(lái)就是使用了,給每一個(gè)不同的TabBarItem的插槽寫(xiě)入不同的圖片和文字信息。

//MainTabBar.vue
<template>
  <div class="mainTabBar">
    <tab-bar>
      <tab-bar-item path="/home" activeColor="#ff8198">
        <img src="~assets/img/tabbar/home.svg" alt="" slot="item-icon">
        <img src="~assets/img/tabbar/home_active.svg" alt="" slot="item-icon-active">
        <div slot="item-text">首頁(yè)</div>
      </tab-bar-item>
      <tab-bar-item path="/category" activeColor="#ff8198">
        <img src="~assets/img/tabbar/category.svg" alt="" slot="item-icon">
        <img src="~assets/img/tabbar/category_active.svg" alt="" slot="item-icon-active">
        <div slot="item-text">分類(lèi)</div>
      </tab-bar-item>
      <tab-bar-item path="/cart" activeColor="#ff8198">
        <img src="~assets/img/tabbar/shopcart.svg" alt="" slot="item-icon">
        <img src="~assets/img/tabbar/shopcart_active.svg" alt="" slot="item-icon-active">
        <div slot="item-text">購(gòu)物車(chē)</div>
      </tab-bar-item>
      <tab-bar-item path="/profile" activeColor="#ff8198">
        <img src="~assets/img/tabbar/profile.svg" alt="" slot="item-icon">
        <img src="~assets/img/tabbar/profile_active.svg" alt="" slot="item-icon-active">
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

<script>
import TabBar from "components/common/tabbar/TabBar"
import TabBarItem from "components/content/tabbar/TabBarItem"
export default {
  name:"MainTabBar",
  components:{
    TabBar,
    TabBarItem
  }
}

</script>
<style>

</style>

導(dǎo)航欄一般都在主頁(yè)中使用,所以我們把這個(gè)導(dǎo)航欄放在App.vue

<template>
  <div id="app">
    <main-tab-bar></main-tab-bar>
  </div>
</template>

<script>
import MainTabBar from "components/content/tabbar/MainTabBar";
export default {
  name: 'App',
  components:{
    MainTabBar
  }
}

總結(jié):這樣看來(lái),我們寫(xiě)一個(gè)導(dǎo)航欄用了3個(gè)文件,這可能看起來(lái)是麻煩的,但這也大大提高了代碼的可讀性,如果我們還需要在該項(xiàng)目別的地方使用導(dǎo)航欄,我們只需要直接創(chuàng)建一個(gè)MainTabBar類(lèi)似的文件,然后把你要的圖片和文字寫(xiě)進(jìn)入即可,甚至于在別的項(xiàng)目用到時(shí),我們可以直接將文件拷貝過(guò)去就能夠直接使用,連CSS樣式都不需要我們?nèi)?xiě),這就大大提高了我們的開(kāi)發(fā)效率。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論