vant 自义定 tabBar 图标和颜色

本文展示了如何在Vue项目中使用vant组件库自定义底部导航栏(TabBar)的图标和颜色。通过在组件中引入TabBar并设置data属性,动态地改变每个Tab的正常状态和选中状态的图标。同时,使用自定义的CSS样式修改选中时的颜色。代码示例包括模板、脚本部分以及运行效果截图。
摘要由CSDN通过智能技术生成

vant 自义定 tabBar 图标和颜色

1.index代码:

<template>
  <div class="app-container">

    <div class="layout-content">


      <keep-alive>
        <router-view v-if="$route.meta.keepAlive" style="margin-bottom: 50px"/>
      </keep-alive>
      <router-view v-if="!$route.meta.keepAlive" style=" margin-bottom: 50px"/>

    </div>


    <!--    底部导航组件   -->
    <div class="layout-footer">
      <TabBar :data="tabbars" @change="handleChange"/>
    </div>

  </div>
</template>

<script>
import TabBar from '@/components/TabBar'

export default {
  name: 'Home',
  computed: {
    key() {
      return this.$route.path
    }
  },

  data() {

    return {
      pic: '@src/assets/images/home-black.png',
      cachedViews: 'Home',
      tabbars: [
        {
          title: '首页',
          to: {
            name: 'Home'
          },
          normal: require("../../src/assets/images/home_black.png"),
          active: require("../../src/assets/images/home_selected.png")
        },
        {
          title: '模块',
          to: {
            name: 'Model'
          },
          normal: require("../../src/assets/images/model_black.png"),
          active: require("../../src/assets/images/model_selected.png")
        },
        {
          title: '关于我',
          to: {
            name: 'Mine'
          },
          normal: require("../../src/assets/images/mine_black.png"),
          active: require("../../src/assets/images/mine_selected.png")
        }
      ]
    }
  },
  components: {
    TabBar
  },
  methods: {
    handleChange(v) {
      console.log('tab value:', v)
    }
  }
}
</script>

2.TabBar组件代码

<template>
  <div>
    <van-tabbar fixed route v-model="active" @change="handleChange">
      <van-tabbar-item v-for="(item, index) in data" :to="item.to" :icon="item.icon" :key="index">
        <span :class="defaultActive === index ? active:''">{{ item.title }}</span>
        <template slot="icon" slot-scope="props">
          <img :src="props.active ? item.active : item.normal">
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
export default {
  name: 'TabBar',
  props: {
    defaultActive: {
      type: Number,
      default: 0
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      active: this.defaultActive
    }
  },
  methods: {
    handleChange(value) {
      this.$emit('change', value)
    }
  }
}
</script>


<style scoped>
.active_tab img {
  width: 26px;
  height: 26px;
}

/* 自定义选中的颜色 */
.van-tabbar-item--active {
  color: #d81e06;
}
</style>

3.运行效果图
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值