1.先循环数组 实现列表展示
2.添加点击事件
<div class="listTop"
:class="{ 'tree-btn-box-hover' : item.focus }"
v-for="(item, index) in arrayList"
:key="index"
@click="changeName(item.id, index)">
{{ item.name }}
</div>
样式 颜色的值 (动态)
.tree-btn-box-hover { background-color: #3D8AF2 !important; color: #fff !important; }
3.数组 赋值阶段 添加一个 focus 默认false 然后再循环 默认第一条的 focus 为 true
arrayList.value.forEach((item: any) => {
item.focus = false
})
for (let i = 0; i < arrayList.value.length; i++) {
arrayList.value[0].focus = true
}
4.点击事件的时候 要把下标带上 通过 index 和 i 对比 选中的为 true 否则为 false
const changeName = async (id: any, index: any) => { // 点击列表某一条
for (let i = 0; i < arrayList.value.length; i++) {
index === i ? arrayList.value[i].focus = true : arrayList.value[i].focus = false
}
}