vue 組件式開發tabBar

29 篇文章 0 订阅

 如下圖  是想實現的效果

對此組件進行分析

 tabbaritem.vue代碼塊

<template>
<div class="tab-bar-item">
<div v-if="!isActive" @click="itemClick">
  <slot name="item-icon"></slot>
</div>
<slot v-else name="item-icon"></slot>
<div :style="activeStyle">
  <slot name="item-text"></slot>
</div>
</div>
  
</template>

  
<style scoped>
.tab-bar-item{
  flex:1;
  text-align: center;
  height: 49px;
}

tabbar.vue代碼塊

<template>
<div id="tab-bar">
  <slot></slot>
</div> 
</template>

<script>
import TabBarItem from "./TabBarItem.vue"
export default {
  name:'TabBar',
  components:{
    TabBarItem
  }
  
}
</script>

<style scoped>
#tab-bar{
  display: flex;
  flex-direction: row;
  background-color: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 65px;
  border: 1px solid #cccccc;
}

</style>

app.vue代碼塊

<template>
  <div id="app">  
    <router-view></router-view>
    <tab-bar>
      <tab-bar-item path="/home" activeColor="red">
      <img src="./assets/img/首页.png" alt="" slot="item-icon"/>
      <div slot="item-text">首頁</div>
      </tab-bar-item>
      <tab-bar-item>
      <img src="./assets/img/会员.png" alt="" slot="item-icon"/>
      <div slot="item-text">会员</div>
      </tab-bar-item>
      <tab-bar-item>
      <img src="./assets/img/扫一扫.png" alt="" slot="item-icon"/>
      <div slot="item-text">扫一扫</div>
      </tab-bar-item>
      <tab-bar-item>
      <img src="./assets/img/设置.png" alt="" slot="item-icon"/>
      <div slot="item-text">设置</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

然後需要點擊後圖片和文字都變成紅色由父組件傳過來

 

   //組件部分代碼
<div class="tab-bar-item">
  <div v-if="!isActive" @click="itemClick"><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>

computed:{
    isActive(){
      return this.$route.path.indexOf(this.path) !== -1;
      
    },
    activeStyle(){
      return this.isActive ? {color:this.activeColor}:{}
    }
  }



//引用組件部分代碼
<tab-bar-item class="tab-bar-item" path="/home" activeColor="#d81e06" >
      <img src="~assets/img/首页.png" alt="" slot="item-icon"/>
      <img src="~assets/img/首页_active.png" alt="" slot="item-icon-active"/>
      <div slot="item-text">首頁</div>
    </tab-bar-item>


具體代碼可以到github下載

https://github.com/null2014/vue-components

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值