在一个组件模板中调用这个组件本身
right.pngdata.json
<style>
img{
width: 12px;
opacity: 0.5;
}
img.active{
transform: rotate(90deg);
opacity: 1;
}
ul{
overflow: hidden;
margin: 0;
}
/* 子列表展开闭合动画 */
.v-enter, .v-leave-to{
max-height: 0;
}
.v-enter-active, .v-leave-active{
transition: 1s;
}
.v-enter-to, .v-leave{
max-height: 200px;
}
</style>
<body>
<script src='https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js'></script>
<div id='myApp'>
<h1>递归组件</h1>
<my-com v-for="item in list" :data="item" :key="item.title"></my-com>
</div>
<!-- 组件模板 -->
<template id='myTem'>
<div>
<img
@click="open=!open"
:class="open? 'active': ''"
src="./right.png"
v-if="data.list.length"
>
<b @click="open=!open">
{{data.title}}
<span v-if="data.list.length">({{data.list.length}})</span>
</b>
<transition>
<ul v-if='open'>
<!-- 递归组件: 在一个组件模板中调用这个组件本身 -->
<li v-for="item in data.list" is="my-com" :data="item" :key="item.title"></li>
</ul>
</transition>
</div>
</template>
<script>
Vue.component('myCom', {
template: '#myTem',
props: ["data"],
data(){return { open: false }}
})
new Vue({
el: '#myApp',
data: {
list: []
},
created() {
fetch("./data.json").then(res=>{
return res.json()
}).then(res=>{
this.list = res
})
},
})
</script>
</body>