话不多说,直接展示所有代码,具体注释都在内容里面,开箱即用
<template>
<div class="container">
<span :class="checked === item.id ? 'active' : ''" v-for="(item, index) in btnList" :key="item.id"
@click="changeTabs(item)">{{ item.name }}</span>
<div>
<component :is="current.Name"></component>
</div>
</div>
</template>
<script setup>
import { reactive, ref, markRaw } from 'vue'
import A from '@/components/canChangeCom/comA.vue'
import B from '@/components/canChangeCom/comB.vue'
import C from '@/components/canChangeCom/comC.vue'
const btnList = reactive([
{ id: 1, name: 'A' , comName: markRaw(A)},
{ id: 2, name: 'B' , comName: markRaw(B)},
{ id: 3, name: 'C' , comName: markRaw(C)},
])
let current = reactive({
Name: btnList[0].comName
})
let checked = ref(1)
const changeTabs = (item) => {
checked.value = item.id
current.Name = item.comName
}
</script>
<style lang="scss" scoped>
.container {
width: 100%;
height: 500px;
border: 3px solid #ccc;
span {
display: inline-block;
width: 100PX;
border: 1px solid #ccc;
text-align: center;
margin-left: 10px;
margin-top: 10px;
cursor: pointer;
}
.active {
background: orange;
}
}
</style>