在vue项目中,快速用插槽实现tabbar导航,只要在页面在中写具体业务逻辑,实现敏捷开发。
这是TabBar的组件代码:
<template>
<div id="tab-bar">
<!-- 用插槽实现tabbar -->
<slot></slot>
</div>
</template>
<script>
export default {
name: "TabBar"
};
</script>
<style scoped>
#tab-bar {
display: flex;
background: #f6f6f6;
position: fixed;
left: 0;
right: 0;
bottom: 0;
box-shadow: 0 -1px 1px rgba(100, 100, 100, 0.2);
justify-content: space-around;
}
</style>
这是TabBarItem的组件代码:
<template>
<div>
<div class="tab-bar-item" @click="tabClick">
<!-- 用插槽是实现tabitem -->
<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>
</div>
</div>
</template>
<script>
export default {
name: "TabBarItem",
props: {
path: String,
activeColor: {
type: String,
default: "gray"
}
},
data() {
return {
// isActive: true
};
},
computed: {
isActive() {
return this.$route.path.indexOf(this.path) !== -1;
},
activeStyle() {
return this.isActive ? { color: this.activeColor } : {};
}
},
methods: {
tabClick() {
console.log("methods");
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: 3px;
vertical-align: middle;
margin-bottom: 3px;
}
</style>
只需要在app.vue配置你需要的几个导航,例子:
<template>
<div id="app">
<tab-bar>
<!-- 这里只要增加tabbar就行,组件内部不需要修改 -->
<tab-bar-item path="/home" activeColor="red">
<img slot="item-icon" src="./assets/img/tabbar/main.png" alt="" />
<img slot="item-icon-active" src="./assets/img/tabbar/selectedmain.png" alt="">
<div slot="item-text">首页</div>
</tab-bar-item>
<tab-bar-item path="/message" activeColor="red">
<img slot="item-icon" src="./assets/img/tabbar/message.png" alt="" />
<img slot="item-icon-active" src="./assets/img/tabbar/selectedmessage.png" alt="">
<div slot="item-text">消息</div>
</tab-bar-item>
<tab-bar-item path="/mine" activeColor="red">
<img slot="item-icon" src="./assets/img/tabbar/mine.png" alt="" />
<img slot="item-icon-active" src="./assets/img/tabbar/selectedmine.png" alt="">
<div slot="item-text">我的</div>
</tab-bar-item>
</tab-bar>
<router-view></router-view>
</div>
</template>
<script>
/* 引入组件 */
import TabBar from "./components/tabbar/TabBar";
import TabBarItem from "./components/tabbar/TabBarItem";
export default {
name: "App",
components: {
TabBar,
TabBarItem
}
};
</script>
<style>
/* 在style中引入样式方法 */
@import "./assets/css/base.css";
</style>
activeColor=“red” 传入选中的颜色,颜色根据自己需求填, TabBar组件和TabBarItem都不需要改动,就能快速实现底部导航。
我把完整的代码附上。
https://download.csdn.net/download/qq_14948637/12155611