递归组件自己调用自己,子数据如何显示呢?
来源:1-2 Vue项目详情页 - 公用图片画廊组件拆分(1)
unbreakable_全栈
2020-12-05 23:01:19
# 具体遇到的问题
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
<template>
<div>
<detail-banner :bannerImg="bannerImg" :sightName="sightName" :gallaryImgs="gallaryImgs"></detail-banner>
<detail-header></detail-header>
<div class="content">
<!-- <detail-list :categoryList="categoryList"></detail-list> -->
<detail-list></detail-list>
</div>
</div>
</template>
<script>
import DetailBanner from './components/Banner'
import DetailHeader from './components/Header'
import DetailList from './components/List'
import axios from 'axios'
import bus from '../../bus'
export default {
name: 'detail', // name 的用处一: 递归组件 用处二: 对某个页面取消缓存的时候 exclude="detail"
data () {
return {
bannerImg: '',
sightName: '',
gallaryImgs: []
// categoryList: []
}
},
components: {
DetailBanner,
DetailHeader,
DetailList
},
methods: {
getDetailInfo () {
axios.get('/api/detail.json', {
params: {
id: this.$route.params.id
}
}).then(this.getDetailInfoSucc)
},
getDetailInfoSucc (res) {
const data = res.data
if (data.ret && data.data) {
const res = data.data
this.bannerImg = res.bannerImg
this.sightName = res.sightName
this.gallaryImgs = res.gallaryImgs
// this.categoryList = res.categoryList
bus.$emit('categoryListData', res.categoryList)
}
}
},
mounted () {
this.getDetailInfo()
}
}
</script>
<style lang="stylus" scoped>
.content
height 50rem
</style>
-----------------------------------
<template>
<div class="detail-list">
<div class="item" v-for="(item, index) in this.categoryList" :key="index">
<div class="item-title border-bottom">
<span class="item-title-icon"></span>
{{item.title}}
</div>
<div class="item-children" v-if="item.children">
<!-- 递归组件 自己调用自己 -->
<!-- <detail-list :categoryList="item.children"></detail-list> -->
<detail-list></detail-list>
</div>
</div>
</div>
</template>
<script>
import bus from '../../../bus'
export default {
name: 'detailList',
// props: {
// categoryList: {
// type: Array
// }
// },
data () {
return {
categoryList: []
}
},
mounted () {
bus.$on('categoryListData', (val) => {
this.categoryList = val
})
}
}
</script>
<style lang="stylus" scoped>
.detail-list
.item
.item-title
line-height .8rem
font-size .32rem
padding 0 .2rem
.item-title-icon
position relative
top .06rem
left .06rem
display inline-block
width .36rem
height .36rem
margin-right .1rem
background url(http://s.qunarzz.com/piao/image/touch/sight/detail.png) 0 -.45rem no-repeat
background-size .4rem 3rem
.item-children
padding 0 .2rem
</style>
1回答
好帮手慕久久
2020-12-06
同学你好,问题解答如下:
递归组件是指自己内部还会使用自己,就好像套娃一样,自己内部再套一个自己。使用的时候,和普通的子组件一样,正常传数据即可,比如list组件本身(称为A)需要父组件传入list这个数据,那么只要给嵌套的那个自己(称为B)也传入list即可,即B的数据是A的数据list中的一部分,如下:
由于A、B的html结构是一样的,数据格式也是一样的,所以B的数据,显示方式也和A的一样,如下:
祝学习愉快!
相似问题