data.json
<body>
<style>
img{
width: 12px;
opacity: 0.5;
}
img.active{
transform: rotate(90deg);
opacity: 1;
}
</style>
<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>
<ul v-if='open'>
<!-- 递归组件: 在一个组件模板中调用这个组件本身 -->
<li v-for="item in data.list" is="my-com" :data="item" :key="item.title"></li>
</ul>
</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>
right.pngdata.json