通过v-show和@click来控制收起展开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
#app {
width: 400px;
margin: 100px auto;
}
.tilte {
height: 35px;
line-height: 35px;
border: 1px solid #ccc;
padding: 8px;
}
h3 {
display: inline-block;
}
.tilte span {
float: right;
cursor: pointer;
}
.content {
border: 1px solid #ccc;
padding: 10px;
}
.content p {
padding: 2px;
}
</style>
</head>
<body>
<div id="app">
<div class="tilte">
<h3>{{title}}</h3>
<span @click="toggleExpanded">{{ expanded ? '收起' : '展开' }}</span>
</div>
<div class="content" v-show="expanded">
<p v-for="(item,index) in content">{{ item }}</p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
expanded: false,
title: '芙蓉楼送辛渐',
content: [
' 寒雨连江夜入吴,',
'平明送客楚山孤。',
'洛阳亲友如相问,',
' 一片冰心在玉壶。'
],
},
methods: {
toggleExpanded() {
this.expanded = !this.expanded
}
}
})
</script>
</body>
</html>