一、TabBar实现思路

  1. 定义一个TabBar组件
  2. TabBar中显示的内容由外界决定
    1. 定义插槽

二、assets文件夹

assets文件夹里面存放的是一些css文件和img文件

assets文件夹里面创建一个base.css, 配置取消网页默认的paddingmargin值,

2.1 在App.vue里面动态引用css文件

<style>
  @import "./assets/css/base.css";
  @import "./assets/css/tab_bar.css";
</style>

三、基本搭建

App.vue

<template>
  <div id="app">
    <div id="tab-bar">
      <div class="tab-bar-item">首页</div>
      <div class="tab-bar-item">分类</div>
      <div class="tab-bar-item">购物车</div>
      <div class="tab-bar-item">我的</div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
}
</script>

<style>
  @import "./assets/css/base.css";
  @import "./assets/css/tab_bar.css";
</style>

tab_bar.css

#tab-bar {
  display: flex;
  background-color: #f6f6f6;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-shadow: 0 -1px 1px rgba(100, 100, 100, 0.08);
}
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  line-height: 49px;
}

四、TabBar和TabBarItem组件封装

4.1 TabBar组件封装

tabbar的样式和功能提取到TabBar组件里面

<template>
  <div id="tab-bar">
    <div class="tab-bar-item">首页</div>
    <div class="tab-bar-item">分类</div>
    <div class="tab-bar-item">购物车</div>
    <div class="tab-bar-item">我的</div>
  </div>
</template>

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

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

在App.vue里面注册并使用tabbar组件

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

<script>
  import TabBar from './components/tabbar/TabBar'

  export default {
    name: 'App',
    // 注册组件
    components: {
      TabBar
    }
  }
</script>

<style>
  @import "./assets/css/base.css";
</style>

在TabBar组件里面放置一个插槽, 用来放TabBarItem的东西

4.2 TabBarItem组件封装

TabBarItem抽取为一个组件, 然后放到插槽里面.

TabBarItem.vue

<template>
  <div id="tab-bar-item">
    <div class="tab-bar-item">
      <img src="../../assets/img/tabbar/home.svg">
      首页
    </div>
    <div class="tab-bar-item">
      <img src="../../assets/img/tabbar/category.svg">
      分类
    </div>
    <div class="tab-bar-item">
      <img src="../../assets/img/tabbar/shopcart.svg">
      购物车
    </div>
    <div class="tab-bar-item">
      <img src="../../assets/img/tabbar/profile.svg">
      我的
    </div>
  </div>
</template>

<script>
  export default {
    name: 'TabBarItem'
  }
</script>

<style>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    line-height: 49px;
  }
  .tab-bar-item img {
    height: 24px;
    width: 24px;
  }
</style>

五、使用具名插槽完成img和文字的封装

TabBarItem.vue

<template>
    <div class="tab-bar-item">
      <!-- 存放图片 -->
      <slot name="item-icon"></slot>
      <!-- 存放文字 -->
      <slot name="item-text"></slot>
    </div>
</template>

<script>
  export default {
    name: 'TabBarItem'
  }
</script>

<style>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }
  .tab-bar-item img {
    height: 24px;
    width: 24px;
  }
</style>

5.1 在App.vue往TabBarItem插槽里面放图片和文字

<template>
  <div id="app">
    <tab-bar>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/home.svg">
        <div slot="item-text">首页</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/shopcart.svg">
        <div slot="item-text">购物车</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/category.svg">
        <div slot="item-text">分类</div>
      </tab-bar-item>
      <tab-bar-item>
        <img slot="item-icon" src="./assets/img/tabbar/profile.svg">
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

<script>
  import TabBar from './components/tabbar/TabBar'
  import TabBarItem from './components/tabbar/TabBarItem'

  export default {
    name: 'App',
    // 注册组件
    components: {
      TabBar,
      TabBarItem
    }
  }
</script>

<style>
  @import "./assets/css/base.css";
</style>

六、修改底部样式

TabBarItem.vue

<template>
    <div class="tab-bar-item">
      <!-- 存放图片 -->
      <!-- 定义一个变量, 用来记录是否显示 -->
      <slot v-if="isActive" name="item-icon"></slot>
      <!-- 存放点击后改变样式的图片 -->
      <slot v-else name="item-icon-active"></slot>
      <!-- 存放文字 -->
      <!-- 因为替换插槽会直接把slot直接替换, 所以在slot里面定义class也会被替换, 所以定义一个div用来定义class -->
      <div :class="{active: !isActive}"><slot name="item-text"></slot></div>
    </div>
</template>

<script>
  export default {
    name: 'TabBarItem',
    data() {
      return {
        isActive: true
      }
    },
  }
</script>

<style>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }
  .tab-bar-item img {
    height: 24px;
    width: 24px;
  }
  .active {
    color: red;
  }
</style>
Logo

前往低代码交流专区

更多推荐