文章目录

1.根据列表元素索引来控制

1
2
3
4
5
<template v-for="(item, index) in items">
<div @click="isShow(index)">
<div v-show="activeIndex === index"></div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
export default {
data: function() {
return {
activeIndex: -1 // 初始化索引
};
},
methods: {
isShowInfo(index){
this.activeIndex = index
}
}
}

2.将列表每一项创建为单独的一个组件,然后每个组件都有自己独立的标识

1
2
3
4
5
6
7
8
9
10
11
<!--父组件-->
<template v-for="(item, index) in items">
<child></child>
</template>
<!--子组件-->
<template v-for="(item, index) in items">
<div @click="isShow">
<div v-show="active"></div>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
export default{
data() {
return {
isActive: false
}
},
methods: {
isShow() {
this.isActive = !this.isActive;
}
}
}

学习笔记,根据自己理解简单整理的,不对的地方望指正,非常感谢。

文章目录