TabBar实现思路
实现代码
TabBar.vue
<template>
<div class="tab-bar">
<slot></slot>
</div>
</template>
<script>
export default {
name: 'TabBar',
data() {
return {
}
}
}
</script>
<style scoped>
.tab-bar{
display: flex;
background-color: #f6f6f6;
position: fixed;
left: 0;
right: 0;
bottom: 0;
box-shadow: 0 -3px 1px rgba(100, 100, 100, 0.1);
}
</style>
TabBarItem.vue
<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>
<div :style="activeStyle"><slot name = "item-text"></slot></div>
<!-- <img src = "../../assets/img/tabbar/home.svg" alt="">
<div>首页</div> -->
</div>
</template>
<script>
export default {
name: 'TabBarItem',
props: {
path: String,
activeColor:{
type: String,
default: 'red',
}
},
data() {
return {
// isActive: false
}
},
computed:{
isActive(){
return this.$route.path.indexOf(this.path) !== -1
},
activeStyle(){
return this.isActive ? {color:this.activeColor}:{}
}
},
methods:{
itemClick(){
if(this.$route.path != this.path)
{ this.$router.replace(this.path)}
}
}
}
</script>
<style scoped>
.tab-bar-item{
flex: 1;
text-align: center;
height: 49px;
}
.tab-bar-item img{
width:24px;
height: 24px;
margin-top: 3px;
vertical-align: middle;
}
.active{
color: blueviolet;
}
</style>
MainTabBar.vue
<template>
<div class="main-tab-bar">
<tab-bar>
<tab-bar-item path="/home" activeColor="blue">
<img slot="item-icon" src = "~assets/img/tabbar/home.svg">
<img slot="item-icon-active" src = "~assets/img/tabbar/home-active.svg">
<div slot="item-text">首页</div>
</tab-bar-item>
<tab-bar-item path="/category" activeColor="blue">
<img slot="item-icon" src = "~assets/img/tabbar/category.svg">
<img slot="item-icon-active" src = "~assets/img/tabbar/category-active.svg">
<div slot="item-text">分类</div>
</tab-bar-item>
<tab-bar-item path="/shopcart" activeColor="blue">
<img slot="item-icon" src = "~assets/img/tabbar/shopcart.svg">
<img slot="item-icon-active" src = "~assets/img/tabbar/shopcart-active.svg">
<div slot="item-text">购物车</div>
</tab-bar-item>
<tab-bar-item path="/profile" activeColor="blue">
<img slot="item-icon" src = "~assets/img/tabbar/profile.svg">
<img slot="item-icon-active" src = "~assets/img/tabbar/profile-active.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: 'MainTabBar',
data() {
return {
}
},
components: {
TabBar,
TabBarItem
}
}
</script>
<style lang="" scoped>
</style>
App.vue
<template>
<div id="app">
<router-view></router-view>
<main-tab-bar></main-tab-bar>
</div>
</template>
<script>
import MainTabBar from './components/MainTabBar'
export default {
name: 'App',
components: {
MainTabBar
}
}
</script>
<style scoped>
@import "./assets/css/base.css";
</style>
效果
修改导入文件路径
build/webpack.base.conf.js