vue自定义底部导航栏Tabbar

使用vue封装一个如下图所示的组件
在这里插入图片描述

主要的项目结构如下:
在这里插入图片描述

component/tabbar中新建组件TabBar.vue,内容如下:

<template>
  <div class="tab-bar">
    <slot></slot>
  </div>
</template>

<script>
import TabBarItem from './TabBarItem'
  export default {
    name: "TabBar",
  };
</script>

<style>
.tab-bar {
  display: flex;
  position: fixed;
  right: 0;
  left: 0;
  bottom: 0;
  height: 49px;
  box-shadow: 0px -2px 1px rgba(204,204,204,0.5);
}

</style>

component/tabbar中新建组件TabBarItem.vue,内容如下:

<template>
  <div class="tab-bar-item" @click="clickItem">
    <div  v-if = '!isActive'><slot  name = "item-icon"></slot></div>
    <div  v-else ><slot name="item-icon-active"></slot></div>
    <div :style = 'activeStyle'><slot name = "item-content"></slot></div>
   
  </div>
</template>

<script>
  export default {
    name : 'TabBarItem',
    props : {
      path : String,
      //颜色可以自定义,传的值必须是一个字符串
      activeColor: {
        type: String,
        // default: 'red'
      }
    },
    data() {
      return {
        // isActive : true,
      }
    },
    methods : {
      clickItem() {
        this.$router.push(this.path)
      }
    },
    computed : {
      //当isACtive为ture的时候,字体颜色发生改变
      isActive() {
        return this.$route.path.indexOf(this.path) !== -1
      },
      activeStyle() {
        return this.isActive ? {color: this.activeColor} : {}
      }
    }
  };
</script>

<style>
  .tab-bar > .tab-bar-item {
    flex: 1;
    text-align: center;
  }
  .tab-bar  .tab-bar-item  img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle;
    margin-bottom: 2px;
  }
</style>

component/mainTabBar中新建组件mainTabBar.vue,内容如下:

<template>
  <tab-bar>
    <tab-bar-item path="/home" activeColor="#FF5977">
      <img slot="item-icon" src="../../assets/img/home.svg" alt />
      <img slot="item-icon-active" src="../../assets/img//home_active.svg" alt />
      <div slot="item-content">首页</div>
    </tab-bar-item>
    <tab-bar-item path="/category" activeColor="#FF5977">
      <img slot="item-icon" src="../../assets/img/category.svg" alt />
      <img slot="item-icon-active" src="../../assets/img/category_active.svg" alt />
      <div slot="item-content">分类</div>
    </tab-bar-item>
    <tab-bar-item path="/shopCart" activeColor="#FF5977">
      <img slot="item-icon" src="../../assets/img/shopcart.svg" alt />
      <img slot="item-icon-active" src="../../assets/img/shopcart_active.svg" alt />
      <div slot="item-content">购物车</div>
    </tab-bar-item>
    <tab-bar-item path="/profile" activeColor="#FF5977">
      <img slot="item-icon" src="../../assets/img/profile.svg" alt />
      <img slot="item-icon-active" src="../../assets/img/profile_active.svg" alt />
      <div slot="item-content">我的</div>
    </tab-bar-item>
  </tab-bar>
</template>

<script>
import TabBar from "../tabbar/TabBar";
import TabBarItem from "../tabbar/TabBarItem";

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

<style>
</style>

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')
const shopCart = () => 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 :'/shopCart',
    component : shopCart
  },
  {
    path : '/profile',
    component : Profile
  }
];
const router = new VueRouter({
  routes,
  mode : 'history'
})

export default router;

view中配置要显示的内容:内容都是相似的,此处只列举一个

<template>
  <h2 class="title">首页</h2>
</template>

<script>
export default {

}
</script>

<style>
  .title {
    color : orange;
  }
</style>

main.js中的内容:

import Vue from 'vue'
import App from './App.vue'
import router from './router/index'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router,
}).$mount('#app')

app.vue中的内容:

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

<script>
  import MainTabBar from './components/mainTabBar/mainTabBar'

export default {
  name: "App",
  components: {
   MainTabBar
  },
};
</script>

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

assets/cssbase.js中的代码如下:

body {
  margin : 0;
  padding : 0;
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值