vue

如何用vuejs封装一个tabbar?

2020-03-21  本文已影响0人  Oct13_JJP

本文主要用vue封装了一个复用性强的tabbar组件。在需要用到的项目中直接将组件拿过来使用即可。

效果图

效果图.gif

App.vue

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

<script>
  import MainTabBar from './components/MainTabBar.vue'
    export default {
      name: 'App',
      components: {
        MainTabBar
      }
    }
</script>

<style>
  body {
    margin: 0;
    padding: 0;
  }
</style>

MainTabBar.vue

<template>
  <tab-bar>
    <tab-bar-item path="/home">
      <img slot="item-icon" src="../assets/img/tabbar/home.png" alt="">
      <img slot="item-icon_selected" src="../assets/img/tabbar/home_selected.png" alt="">
      <div slot="item-text">首页</div>
    </tab-bar-item>
    <tab-bar-item path="/find">
      <img slot="item-icon" src="../assets/img/tabbar/find.png" alt="">
      <img slot="item-icon_selected" src="../assets/img/tabbar/find_selected.png" alt="">
      <div slot="item-text">发现</div>
    </tab-bar-item>
    <tab-bar-item path="/info">
      <img slot="item-icon" src="../assets/img/tabbar/info.png" alt="">
      <img slot="item-icon_selected" src="../assets/img/tabbar/info_selected.png" alt="">
      <div slot="item-text">消息</div>
    </tab-bar-item>
    <tab-bar-item path="/profile">
      <img slot="item-icon" src="../assets/img/tabbar/profile.png" alt="">
      <img slot="item-icon_selected" src="../assets/img/tabbar/profile_selected.png" alt="">
      <div slot="item-text">我的</div>
    </tab-bar-item>
  </tab-bar>
</template>

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

<style scoped>
</style>

TabBar.vue

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

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

<style scoped>
  #tabbar {
    display: flex;
    background-color: #F5F5F5;
    box-shadow: 0 -1px 1px rgba(100, 100, 100, 0.2);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
  }
</style>

TabBarItem.vue

<template>
    <div class="tabbar-item" @click="itemClick">
        <div v-if="!isSelected">
            <slot name="item-icon"></slot>
        </div>
        <div v-else>
            <slot name="item-icon_selected"></slot>
        </div>
        <div :style="selectedStyle">
            <slot name="item-text"></slot>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'TabBarItem',
        props: {
            path: String,
            selectedColor: {
                type: String,
                default: "#eb2309"
            },
            color: {
                type: String,
                default: "#181818"
            }
        },
        data() {
            return {
            }
        },
        computed: {
            isSelected() {
                return this.$route.path.indexOf(this.path) !== -1
            },
            selectedStyle() {
                return this.isSelected ? { color: this.selectedColor } : { color: this.color }
            }
        },
        methods: {
            itemClick() {
                this.$router.replace(this.path)
            }
        }
    }
</script>

<style scoped>
  .tabbar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
  }
  .tabbar-item img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle;
    margin-bottom: 2px;
  }
  .selected {
    color: #eb2309;
  }
</style>

路由配置 index.js

import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import('../views/home/Home')
const Find = () => import('../views/find/Find')
const Info = () => import('../views/info/Info')
const Profile = () => import('../views/profile/Profile')

// 1. 安装插件
Vue.use(VueRouter)

// 2. 创建路由对象
const routes = [
  {
    path: '',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/find',
    component: Find
  },
  {
    path: '/info',
    component: Info
  },
  {
    path: '/profile',
    component: Profile
  }
]
const router = new VueRouter({
  routes,
  mode: "history"
})

// 3. 导出router
export default router
上一篇 下一篇

猜你喜欢

热点阅读