folders: [
name: 'folder1',
children: [{
name: 'folder1 - folder1',
children: [{
name: 'folder1 - folder1 - folder1'
name: 'folder1 - folder2',
children: [{
name: 'folder1 - folder2 - folder1'
name: 'folder1 - folder2 - folder2'
name: 'folder 2',
children: [{
name: 'folder2 - folder1',
children: [{
name: 'folder2 - folder1 - folder1'
name: 'folder2 - folder2',
children: [{
name: 'folder2-content1'
name: 'folder 3',
children: [{
name: 'folder3 - folder1',
children: [{
name: 'folder3 - folder1 - folder1'
name: 'folder3 - folder2',
children: [{
name: 'folder3-content1'
这个案例有爷爷组件这块我有在全局进行注册,HomeView.vue是爷爷组件,包含数据源,引进父级组件等操作;
main.js全局注册:
HomeView.vue(爷爷组件):
<template>
<div class="home">
<li v-for="folder in folders">
<about-view :folder="folder"></about-view>
</template>
<script>
//引进父组件
import AboutView from './AboutView.vue';
export default {
components:{
AboutView,
data(){
return{
folders: [
name: 'folder1',
children: [{
name: 'folder1 - folder1',
children: [{
name: 'folder1 - folder1 - folder1'
name: 'folder1 - folder2',
children: [{
name: 'folder1 - folder2 - folder1'
name: 'folder1 - folder2 - folder2'
name: 'folder 2',
children: [{
name: 'folder2 - folder1',
children: [{
name: 'folder2 - folder1 - folder1'
name: 'folder2 - folder2',
children: [{
name: 'folder2-content1'
name: 'folder 3',
children: [{
name: 'folder3 - folder1',
children: [{
name: 'folder3 - folder1 - folder1'
name: 'folder3 - folder2',
children: [{
name: 'folder3-content1'
</script>
AboutView.vue(父级组件):
<template>
<div class="about">
<span>{{folder.name}}</span>
<second-view :children="folder.children"></second-view>
</template>
<script>
export default{
props:['folder'],
components:{
// HomeView
SecondView:()=>import('./SecondView.vue')
data(){return{}},
</script>
SecondView.vue(孙子组件):
<template>
<li v-for="child in children">
<about-view v-if="child.children" :folder="child"></about-view>
<span v-else>{{child.name}}</span>
</template>
<script>
// import AboutView from './AboutView.vue'
export default{
props:['children'],
components:{
AboutView:()=>import ('./AboutView.vue')
data(){return{}},
</script>
写这个案例的时候出现了一个报错,是一个引入的逻辑错误,你需要找到一个循环支撑点,而又不能让他陷入无限循环,所以我让爷爷组件在全局注册,让子父组件异步引入,这样就可以解决报错问题,当然解决方法还有其他方式,例如在beaforecreate中引入,利用webpack等,欢迎相互讨论!