基於 vue封裝的 tabbar 組件


一,不確定有幾個tabbar ,可能有三個,可能有四個,或者更多...

二,圖片,文字點擊高亮

三,跳轉頁面

四, 配置路由

雖然這個功能很多ui框架都有,但是封裝也是一個學習的過程。

界面效果

 

 

父組件

<template>
  <div id="app">
    <router-view></router-view>
    <tab></tab>
  </div>
</template>
<script>
import Tab from "./components/Tab";
export default {
  name: "App",
  components: {
    Tab,
  },
  data() {
    return {};
  },
  methods: {},
};
</script>
<style lang="less">
// #115FFB  #909090
@import "./assets/css/common.css";

</style>

子組件我用了三層,一層嵌套一層,耐心看喲,最主要學習插槽,父子組件傳值

第一個子組件  (定義放置tabbar的大容器)

<template>
  <div class="tabbar">
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "TabBar",
};
</script>
<style lang="less">
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  width: 100%;
  height: 45px;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  text-align: center;
}
</style>

第二個子組件(tabbar 中的圖片,文字的動態顯示)

<template>
  <!-- 這里就相當於 每一個 -->
  <div class="tabbar-item" @click="getPath">
    <!-- 未選中圖片 -->
    <div class="tabbar_icon" v-show="!isShow">
      <slot name="icon"> </slot>
    </div>
    <!-- 選中圖片 -->
    <div class="tabbar_icon_active" v-show="isShow">
      <slot name="iconactive"></slot>
    </div>
    <!-- 文字 -->
    <div class="tabbar_title" :style="active">
      <slot name="title"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  // 父向子傳
  props: {
    // 跳轉路徑
    path: {
      type: String,
    },
    // 高亮文字顏色
    activecolor: {
      tyep: String,
    },
  },
  // 計算屬性
  computed: {
    // 如果要檢索的字符串值沒有出現,則該方法返回 -1。 不等於-1表示出現
    isShow() {
      return this.$route.path.indexOf(this.path) != -1;
    },
    active() {
      return this.isShow ? { color: this.activecolor } : "";
    },
  },
  methods: {
    getPath() {
      this.$router.push(this.path); // 跳轉頁面
      //console.log(this.path); 父向子傳值
      //console.log(this.$route.path); vue 獲取路徑
    },
  },
};
</script>
<style lang="less">
.tabbar-item {
  flex: 1;
  height: 100%;
}
.tabbar_title {
  font-size: 14px;
  color: #909090;
}
</style>

第三個子組件(組合tabbar,可以任意放置三個,四個,或更多)

<template>
  <div class="tab">
    <tab-bar>
      <tab-bar-item path="/home" activecolor="#115FFB">
        <img slot="icon" src="../assets/img/home.png" alt="" />
        <img slot="iconactive" src="../assets/img/home_active.png" alt="" />
        <span slot="title">
          首頁
        </span>
      </tab-bar-item>
      <tab-bar-item path="/subject" activecolor="#115FFB">
        <img slot="icon" src="../assets/img/subject.png" alt="" />
        <img slot="iconactive" src="../assets/img/subject_active.png" alt="" />
        <span slot="title">
          視頻
        </span>
      </tab-bar-item>
      <tab-bar-item path="/profile" activecolor="#115FFB">
        <img slot="icon" src="../assets/img/profile.png" alt="" />
        <img slot="iconactive" src="../assets/img/profile_active.png" alt="" />
        <span slot="title">
          我
        </span>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

<script>
import TabBar from "../components/TabBar";
import TabBarItem from "../components/TabBarItem";
export default {
  name: "Tab",
  components: {
    TabBar,
    TabBarItem,
  },
  data() {
    return {};
  },
  created() {},
  methods: {},
};
</script>
<style lang="less"></style>

配置router

const routes = [
  {
    path: "/home",
    name: "home",
    component: home,
  },
  {
    path: "/subject",
    name: "subject",
    component: () => import("../views/subject.vue"),
  },
  {
    path: "/profile",
    name: "profile",
    component: () => import("../views/profile.vue"),
  },
];


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM