Vue封装TabBar组件

Vue环境准备

1、npm install vue-router 因为后面用得到路由
2、阿里矢量库,用于TabBar的图标
在这里插入图片描述

组件抽离为两个

1、TabBar.vue

<template>
    <div id="tab-bar">
      <slot></slot> //插槽:插入多个TabBarItem
    </div>
</template>

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

<style scoped>
   #tab-bar{
     display: flex;
     background-color: #f6f6f6;
     position: fixed;
     left: 0;
     right: 0;
     bottom: 0;
     box-shadow: 0 -1px 1px rgba(100, 100, 100, .2);
   }
</style>

2、TabBarItem

<template>
  <div class="tab-bar-item" @click="itemClick">
      <div v-if="!isActive"><slot name="item-icon"></slot></div>
      <div v-else><slot name="item-icon-active"></slot></div>
      <!--绑定class属性方式-->
      <!-- <div :class="{active: isActive}"><slot name="item-text"></slot></div> -->
      <!--绑定style属性的方式+计算属性-->
      <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
    name: "TabBarItem",
    props: {
      // 接受点击TabBarItem跳转时的路径
      path: String,
      // 接受父组件传来的自定义“激活颜色”
      activeColor: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        // isActive: true
      }
    },
    computed: {
      // 是否点击的是当前TabBarItem
      isActive(){
        return this.$route.path.indexOf(this.path) != -1;
      },
      // 如果点击的是当前TabBarItem,则显示激活样式
      activeStyle(){
        return this.isActive ? {color: this.activeColor} : {}
      }
    },
    methods:{
      itemClick(){
        // 点击tarbitem跳转,跳转路径为“this.path”,不返回
        this.$router.replace(this.path)
      }
    }
}

</script>
<style scoped>
    .tab-bar-item{
      flex: 1;
      text-align: center;
      height: 49px;
      font-size: 14px;
    }
    .tab-bar-item img{
      width: 24px;
      height: 24px;
      margin-top: 1px;
      margin-bottom: 2px;
    }
    /* .active{
      color: #d4237a;
    } */
</style>

3、App.vue 应用上面封装的TabBar

<template>
  <div id="app">
    <router-view></router-view>
    <tab-bar>
      <tab-bar-item path="/home" activeColor="#d4237a">
        <!--这里的:~assets 等同于 src/assets -->
        <img slot="item-icon" src="~assets/img/tabbar/home.png" alt="">
        <img slot="item-icon-active" src="~assets/img/tabbar/home_active.png" alt="">
        <div slot="item-text">首页</div>
      </tab-bar-item>
      <tab-bar-item path="/category" activeColor="#d4237a">
        <img slot="item-icon" src="~assets/img/tabbar/category.png" alt="">
        <img slot="item-icon-active" src="~assets/img/tabbar/category_active.png" alt="">
        <div slot="item-text">分类</div>
      </tab-bar-item>
      <tab-bar-item path="/cart" activeColor="#d4237a">
        <img slot="item-icon" src="~assets/img/tabbar/buy.png" alt="">
        <img slot="item-icon-active" src="~assets/img/tabbar/buy_active.png" alt="">
        <div slot="item-text">购物车</div>
      </tab-bar-item>
      <tab-bar-item path="/profile" activeColor="#d4237a">
        <img slot="item-icon" src="~assets/img/tabbar/profile.png" alt="">
        <img slot="item-icon-active" src="~assets/img/tabbar/profile_active.png" alt="">
        <div slot="item-text">我的</div>
      </tab-bar-item>
    </tab-bar>
  </div>
</template>

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

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

<style scoped>
</style>

4、vue-router 路由跳转 index.js配置

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

const Home = () => import('../views/home/Home.vue')
const Category = () => import('../views/category/Category.vue')
const Cart = () => import('../views/cart/Cart.vue')
const Profile = () => import('../views/profile/Profile.vue')

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

const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        component: Home
    },
    {
        path: '/category',
        component: Category
    },
    {
        path: '/cart',
        component: Cart
    },
    {
        path: '/profile',
        component: Profile
    }
]

const router = new VueRouter({
    routes,
    mode: 'history'
})
// 导出router
export default router

效果展示

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 UniApp 中封装底部 Tabbar,你可以按照以下步骤进行操作: 1. 创建一个公共的 Tabbar 组件,可以使用 `uni-tabbar` 组件或者自定义组件实现。 2. 在 App.vue 文件中,使用 `<template>` 标签包裹 `<uni-tab-bar>` 组件,并设置 tabbar 的数据和事件处理方法。 3. 在每个页面的 vue 文件中,使用 `<template>` 标签包裹页面内容,并在相应的位置添加 `<uni-tab-bar-item>` 组件,设置对应的图标、标题和路径。 4. 在每个页面的 vue 文件中,在 `<script>` 标签中引入 tabbar 组件,并在 `data` 属性中定义 tabbar 的数据和事件处理方法。 5. 在组件的事件处理方法中,根据当前选中的 tabbar 项,通过 `uni.switchTab` 方法跳转到对应的页面。 下面是一个简单的示例: App.vue: ```vue <template> <view> <template> <uni-tab-bar :selected="selectedIndex" @click="switchTab"> <uni-tab-bar-item index="0" icon="home" text="首页" page="/pages/home"></uni-tab-bar-item> <uni-tab-bar-item index="1" icon="list" text="列表" page="/pages/list"></uni-tab-bar-item> <uni-tab-bar-item index="2" icon="user" text="我的" page="/pages/mine"></uni-tab-bar-item> </uni-tab-bar> </template> <router-view></router-view> </view> </template> <script> export default { data() { return { selectedIndex: 0 } }, methods: { switchTab(index) { this.selectedIndex = index uni.switchTab({ url: this.$children.$children[index].page }) } } } </script> ``` Home.vue: ```vue <template> <view> <!-- 页面内容 --> </view> </template> <script> import UniTabBarItem from '@/components/UniTabBarItem.vue' export default { components: { UniTabBarItem }, data() { return { tabbarData: [ { index: 0, icon: 'home', text: '首页', page: '/pages/home' }, { index: 1, icon: 'list', text: '列表', page: '/pages/list' }, { index: 2, icon: 'user', text: '我的', page: '/pages/mine' } ] } }, methods: { switchTab(index) { uni.switchTab({ url: this.tabbarData[index].page }) } } } </script> ``` List.vue 和 Mine.vue 的代码类似,只需要修改对应的页面内容和 tabbarData 数据。 这样,你就可以在页面中使用底部 Tabbar,并实现页面间的切换了。希望能对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值