版权声明:未经本人同意不得私自转载 https://blog.csdn.net/qq_40190624/article/details/85274794
需求:底部有tab切换;点击当前会显示对应的子页面
效果图
tab子组件:
<template>
<div class="tabbar">
<router-link
class="tab-item"
v-for="(item,index) in data"
:key="index"
:to="item.path"
active-class="is-selected"
>
<div class="tab-item-icon">
<i :class="'fa fa-'+item.icon"></i>
</div>
<div class="tab-item-label">
{{item.title}}
</div>
</router-link>
</div>
</template>
<script>
export default {
name: 'tabbar',
props: {
data: Array
}
};
</script>
<style scoped>
.tabbar {
height: 55px;
box-sizing: border-box;
width: 100%;
position: fixed;
bottom: 0;
background-image: linear-gradient(
180deg,
#d9d9d9,
#d9d9d9 50%,
transparent 0
);
background-size: 100% 1px;
background-repeat: no-repeat;
background-position: 0 0;
background-color: #fafafa;
display: flex;
text-align: center;
}
.tab-item {
display: block;
padding: 7px 0;
flex: 1;
}
.tab-item-icon {
width: 24px;
height: 24px;
margin: 0 auto 5px;
}
.tab-item-icon i {
font-size: 1.3rem;
}
.tab-item-label {
color: inherit;
font-size: 12px;
line-height: 1;
}
a {
text-decoration: none;
color: #888;
}
.is-selected {
color: #20af0e;
}
</style>
创建四个页面:
Chats.vue,Contacts.vue,Discover.vue,Me.vue
路由页面:
import Index from './views/Index.vue';
Vue.use(Router);
const router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'index',
redirect: '/chats',//这里给一个重定向(默认)的路径是因为页面一进入的时候有一个图标需要有选中状态
component: Index,
children: [
{
path: '/chats',
name: 'chats',
component: () => import('./views/Chats.vue')
},
{
path: '/contacts',
name: 'contacts',
component: () => import('./views/Contacts.vue')
},
{
path: '/discover',
name: 'discover',
component: () => import('./views/Discover.vue')
},
{
path: '/me',
name: 'me',
component: () => import('./views/Me.vue')
}
]
},)
index.vue页面配置
<template>
<div class="index">
<!--组件支持用户在具有路由功能的应用中点击导航-->
<router-view></router-view>
<TabBar :data="tabbarData"/>
</div>
</template>
<script>
import TabBar from '../components/TabBar';
export default {
name: 'index',
data() {
return {
tabbarData: [
//这里的icon是引入的字体图标库的图标,你们可以写img路径也可以,根据实际情况修改
{ title: '微信', icon: 'comment', path: '/chats' },
{ title: '通讯录', icon: 'address-book', path: '/contacts' },
{ title: '发现', icon: 'compass', path: '/discover' },
{ title: '我', icon: 'user', path: '/me' }
]
};
},
components: { TabBar }
};
</script>
<style scoped>
.index {
width: 100%;
height: 100%;
}
</style>
子页面写法:(这里只写一页面;其它三个页面写法类似就不粘上来了)
<template>
<div>
<Header title="微信" btn_icon="plus"/>
</div>
</template>
<script>
import Header from '../components/Header';
export default {
name: 'tabbar',
components: {
Header
}
};
</script>