欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue遞歸組件實(shí)現(xiàn)樹(shù)形結(jié)構(gòu)

 更新時(shí)間:2022年09月01日 10:50:37   作者:依古比古*  
這篇文章主要為大家詳細(xì)介紹了vue遞歸組件實(shí)現(xiàn)樹(shù)形結(jié)構(gòu),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了vue遞歸組件實(shí)現(xiàn)樹(shù)形結(jié)構(gòu),供大家參考,具體內(nèi)容如下

一、遞歸組件

什么是遞歸組件?簡(jiǎn)單來(lái)說(shuō)就是在組件中內(nèi)使用組件本身。函數(shù)自己調(diào)用自己。很多情況下我們呢刷數(shù)據(jù)的時(shí)候,不知道到底這個(gè)數(shù)據(jù)結(jié)構(gòu)是有多少層,那么這個(gè)時(shí)候我們就用到了遞歸來(lái)實(shí)現(xiàn)。

二、先用for來(lái)遍歷:

父組件中:

<template>
? <div class="home">
??
? ? <tree :title="list.name" :list="list.children"></tree>
? </div>
</template>

<script>
import tree from '../components/tree'
export default {
? name: "Home",
? components: {
? ? tree
? },
? data: function() {
? ? return {
? ? ? list: {
? ? ? ? name: "酒店",

? ? ? ? children: [
? ? ? ? ? {
? ? ? ? ? ? name: "經(jīng)濟(jì)",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "如家",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "長(zhǎng)江路-如家"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-如家"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "7天",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "長(zhǎng)江路-7天"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-7天"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? name: "舒適",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "智選假日",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-智選假日"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-智選假日"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "全季",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-全季"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "建設(shè)路-全季"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? name: "商務(wù)",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "中方商務(wù)",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-中方商務(wù)"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-中方商務(wù)"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "錦江之星",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "人民路-錦江之星"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "新華路-錦江之星"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? }
? ? ? ? ]
? ? ? }
? ? };
? },
? methods: {}
};
</script>
<style scoped>
.ww {
? margin-left: 20px;
}
.hh {
? font-size: 10px;
}
</style>

子組件中:

<!-- ?-->
<template>
? <div>

? <!-- 第一層循環(huán) 循環(huán)酒店-->
? ? ?<div v-for="(item,index) in list " :key="index">
? ? ? <h3>{{item.name}}</h3>
? ? <!-- <! 第二層循環(huán)酒店的類型-->?
?
? ? <div v-for="(item,index) in item.children" :key="index">
? ? ? <h5>{{item.name}}</h5>
? ? ? <!-- 第三層循環(huán)酒店的名稱 -->
? ? ? <div v-for="(item,index) in item.children" :key="index" class="ww">
? ? ? ? <h6>{{item.name}}</h6>
? ? ? ? <!-- 第四層循環(huán)酒店的具體介紹 ?-->
? ? ? ? <div v-for="(item,index) in item.children" :key="index" class="hh">{{item.name}}</div>
? ? ? </div>
? ? </div>
? ?</div>?


? </div>
</template>

<script>
export default {
? name: "tree",
? components: {},
? data: function() {
? ? return {
? ? ??
? ? };
? },
? props:{
? ? title:
? ? {
? ? ? type: String,
? ? ? default:"標(biāo)題"

? ? },
? ? list:{
? ? ? type:Array,

? ? }
? }
??
};
</script>
<style scoped>
/* @import url(); 引入css類 */
</style>

效果圖如下:  

三、遞歸來(lái)實(shí)現(xiàn)?

通過(guò)組件自身的 name名字來(lái)實(shí)現(xiàn)組件自身的遞歸調(diào)用 .

父組件中:

<template>
? <div class="home">
??
? ?<tree :title="list.name" :list="list.children" :detph="0"></tree>
? </div>
</template>

<script>
import tree from '../components/tree'
export default {
? name: "Home",
? components: {
? ? tree
? },
? data: function() {
? ? return {
? ? ? list: {
? ? ? ? name: "酒店",

? ? ? ? children: [
? ? ? ? ? {
? ? ? ? ? ? name: "經(jīng)濟(jì)",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "如家",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "長(zhǎng)江路-如家"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-如家"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "7天",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "長(zhǎng)江路-7天"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-7天"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? name: "舒適",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "智選假日",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-智選假日"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-智選假日"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "全季",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-全季"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "建設(shè)路-全季"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? },
? ? ? ? ? {
? ? ? ? ? ? name: "商務(wù)",
? ? ? ? ? ? children: [
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "中方商務(wù)",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "臥龍路-中方商務(wù)"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "望江路-中方商務(wù)"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? },
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? name: "錦江之星",
? ? ? ? ? ? ? ? children: [
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "人民路-錦江之星"
? ? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? name: "新華路-錦江之星"
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? ? }
? ? ? ? ]
? ? ? }
? ? };
? },
? methods: {}
};
</script>
<style scoped>
.ww {
? margin-left: 20px;
}
.hh {
? font-size: 10px;
}
</style>

子組件:

<!-- ?-->
<template>
? <div>
? ? <div :style="getDetph">
? ? ? <!-- 顯示隱藏 -->
? ? ? <div @click="getData ">
? ? ? ? <!-- 這里呢如果為真的話就顯示那么就是減號(hào)否則的話就是加號(hào) -->
? ? ? ? <span >{{isShow?'-':'+'}}</span>{{title}}</div>
? ? ? <!-- 那么在這里我們就需要改成 item.name和item.children -->
? ? ? <div v-if="isShow">
? ? ? ? <tree
? ? ? ? ? :title="item.name"
? ? ? ? ? :list="item.children"
? ? ? ? ? v-for="(item,index) in list"
? ? ? ? ? :key="index"
? ? ? ? ? :detph="detph+1"
? ? ? ? >
? ? ? ? ? <!-- 每次遞增1 -->
? ? ? ? </tree>
? ? ? </div>
? ? </div>
? </div>
</template>

<script>
export default {
? name: "tree",
? components: {},
? data: function() {
? ? return {
? ? ? isShow: false
? ? };
? },
? props: {
? ? // 接收標(biāo)題
? ? title: {
? ? ? type: String
? ? ? // default: "標(biāo)題"
? ? },
? ? // 接收一整個(gè)數(shù)組
? ? list: {
? ? ? type: Array
? ? },
? ? // 用來(lái)接收層數(shù)
? ? detph: {
? ? ? type: Number
? ? }
? },
? // 計(jì)算屬性用來(lái)接收我們的層數(shù)
? computed: {
? ? getDetph() {
? ? ? return `transform:translate(${this.detph * 20}px)`;
? ? }
? },
? methods: {
? ? getData: function() {
? ? ? this.isShow = !this.isShow;
? ? }
? }
};
</script>
<style scoped>
/* @import url(); 引入css類 */
</style>

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue全局事件總線和訂閱與發(fā)布使用案例分析講解

    Vue全局事件總線和訂閱與發(fā)布使用案例分析講解

    在?vue?里我們可以通過(guò)全局事件總線來(lái)實(shí)現(xiàn)任意組件之間通信,它的原理是給?Vue?的原型對(duì)象上面添加一個(gè)屬性。這樣的話我所有組件的都可以訪問(wèn)到這個(gè)屬性,然后可以通過(guò)這個(gè)屬性來(lái)訪問(wèn)其他組件給這個(gè)屬性上面綁定的一些方法,從而去傳遞數(shù)據(jù)
    2022-08-08
  • vue實(shí)現(xiàn)兩個(gè)區(qū)域滾動(dòng)條同步滾動(dòng)

    vue實(shí)現(xiàn)兩個(gè)區(qū)域滾動(dòng)條同步滾動(dòng)

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)兩個(gè)區(qū)域滾動(dòng)條同步滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • django使用channels2.x實(shí)現(xiàn)實(shí)時(shí)通訊

    django使用channels2.x實(shí)現(xiàn)實(shí)時(shí)通訊

    這篇文章主要介紹了django使用channels2.x實(shí)現(xiàn)實(shí)時(shí)通訊,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • 淺談vue中.vue文件解析流程

    淺談vue中.vue文件解析流程

    這篇文章主要介紹了淺談vue中.vue文件解析流程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • vue.js異步上傳文件前后端實(shí)現(xiàn)代碼

    vue.js異步上傳文件前后端實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了vue.js異步上傳文件前后端的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Vue.js 利用v-for中的index值實(shí)現(xiàn)隔行變色

    Vue.js 利用v-for中的index值實(shí)現(xiàn)隔行變色

    這篇文章主要介紹了Vue.js 利用v-for中的index值實(shí)現(xiàn)隔行變色效果,首先定義好樣式,利用v-for中的index值,然后綁定樣式來(lái)實(shí)現(xiàn)隔行變色,需要的朋友可以參考下
    2018-08-08
  • vue使用smooth-signature實(shí)現(xiàn)移動(dòng)端橫豎屏電子簽字

    vue使用smooth-signature實(shí)現(xiàn)移動(dòng)端橫豎屏電子簽字

    這篇文章主要為大家介紹了vue使用smooth-signature實(shí)現(xiàn)移動(dòng)端橫豎屏電子簽字示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)

    Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)

    這篇文章主要介紹了Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)的相關(guān)資料,需要的朋友可以參考下
    2018-02-02
  • vue iview的菜單組件Mune 點(diǎn)擊不高亮的解決方案

    vue iview的菜單組件Mune 點(diǎn)擊不高亮的解決方案

    今天小編就為大家分享一篇vue iview的菜單組件Mune 點(diǎn)擊不高亮的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue實(shí)現(xiàn)分割驗(yàn)證碼效果

    vue實(shí)現(xiàn)分割驗(yàn)證碼效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)分割驗(yàn)證碼效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論