<template>
<div class="tabs">
<div class="tab-header">
<div
v-for="(tab, index) in tabs"
:key="index"
:class="['tab-item', { active: activeTab === index }]"
@click="activeTab = index"
>
{{ tab.title }}
</div>
</div>
<div class="tab-content">
<component :is="tabs[activeTab].content"></component>
</div>
</div>
</template>
<script>
export default {
data() {
return {
activeTab: 0,
tabs: [
{
title: 'Home',
content: 'HomeContent'
},
{
title: 'About',
content: 'AboutContent'
},
{
title: 'Contact',
content: 'ContactContent'
}
]
};
},
components: {
HomeContent: {
template: '<div>Welcome to the Home Tab!</div>'
},
AboutContent: {
template: '<div>Learn more about us on the About Tab.</div>'
},
ContactContent: {
template: '<div>Contact us through the Contact Tab.</div>'
}
}
};
</script>
<style scoped>
.tab-header {
display: flex;
}
.tab-item {
padding: 10px;
cursor: pointer;
}
.tab-item.active {
background-color: #42b983;
color: white;
}
.tab-content {
margin-top: 10px;
padding: 10px;
border: 1px solid #ccc;
}
</style>
模板部分 (<template>):
v-for 指令循环渲染每个标签页的标题。v-bind:class 动态绑定类名,根据当前激活的标签页设置样式。activeTab 的值。<component :is="..."> 动态渲染对应的内容组件。脚本部分 (<script>):
data 函数返回包含 activeTab 和 tabs 数组的对象。tabs 数组中的每个对象包含一个标签页的标题和对应的内容组件名称。HomeContent, AboutContent, ContactContent),每个组件有一个简单的模板。样式部分 (<style scoped>):
下一篇:vue3 createvnode
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站