一个案例
// App.vue
<template>
<div>
<tab-control :titles="titles"
@titleClick="titleClick"></tab-control>
<h2>{{contents[currentIndex]}}</h2>
</div>
</template>
<script>
import TabControl from "./TabControl.vue";
export default {
components:{
TabControl
},
data(){
return {
titles: ["衣服","鞋子","裤子"],
contents: ["衣服页面","鞋子页面","裤子页面"],
currentIndex:0
}
},
methods: {
titleClick(index){
this.currentIndex=index;
}
},
}
</script>
<style scoped>
</style>
// TabControl.vue
<template>
<div class="tab-control">
<div class="tab-control-item"
:class="{active: currentIndex === index}"
v-for="(title, index) in titles"
:key="title"
@click="itemClick(index)">
<span>{{title}}</span>
</div>
</div>
</template>
<script>
export default {
emits:["titleClick"],
props:{
titles:{
type: Array,
default(){
return [];
}
}
},
data(){
return {
currentIndex:0
}
},
methods: {
itemClick(index){
this.currentIndex=index;
this.$emit("titleClick",index);
}
}
}
</script>
<style scoped>
.tab-control{
display: flex;
}
.tab-control-item{
flex:1;
text-align: center;
}
.tab-control-item.active{
color: red;
}
.tab-control-item.active span{
border-bottom: 5px solid red;
padding: 5px 10px;
}
</style>
效果:
