<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          VUE 展示無限層級樹形數(shù)據(jù)結(jié)構(gòu)

          共 8408字,需瀏覽 17分鐘

           ·

          2021-07-05 16:55

          作者:這個少年有點熱、

          來源:SegmentFault 思否社區(qū)


          在做項目中,會遇到一些樹形的數(shù)據(jù)結(jié)構(gòu),常用在左側(cè)菜單導(dǎo)航,或者評論引用等地方,這種數(shù)據(jù)結(jié)構(gòu)有個特點是不知道它會嵌套多少層,所以用template去展示這樣的數(shù)據(jù)時就有點棘手,這篇文章梳理兩種展示這種數(shù)據(jù)結(jié)構(gòu)的方法。

          文章中用到的數(shù)據(jù)是下面這個:

          mainData: {
            value: "root",
            children:[{
              value: "層級1-1",
              children:[{
                value: "層級2-1",
                children:[{
                    value: "層級3-1",
                    children:[]
                 }]
               },{
                 value: "層級2-2",
                 children:[]
               }]
             },{
                value: "層級1-2",
                children:[]
             }]
          }

          也就是下面這個樣子。


          組件遞歸調(diào)用

          第一種是組件遞歸調(diào)用自己的方式,創(chuàng)建一個組件,該組件在引用自己去展示children的數(shù)據(jù),子組件如下:

          <template>
          <div>
            <div class="demo">
              {{treeData.value}}
              <tree-comp v-for="(item, index) in treeData.children" :treeData="item"></tree-comp>
            </div>
          </div>
          </template>
          <script>
          export default {
            name: 'treeComp',
            props:{
              treeData: {
                default: function(){
                  return {}
                }
              }
            },
            mounted(){},
            methods:{}
          }
          </script>
          <style lang="less" scoped>
            .demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999;
              &:before{content:'--';display: inline-block;padding:0 4px;}
            }
          </style>

          然后創(chuàng)建父組件,父組件使用子組件,并將數(shù)據(jù)傳入子組件。

          <template>
            <tree-comp :treeData="mainData"></tree-comp>
          </template>
          <script>
          export default {
            name: 'treeMain',
            data () {
              return {
                mainData: {
                  value: "root",
                  children:[
                    {
                      value: "層級1-1",
                      children:[{
                        value: "層級2-1",
                        children:[{
                          value: "層級3-1",
                          children:[]
                        }]
                      },{
                        value: "層級2-2",
                        children:[]
                      }]
                    },{
                      value: "層級1-2",
                      children:[]
                    }
                  ]
                }
              }
            },
            components:{
              "tree-comp": () =>  import('./TreeComp')
            },
            mounted(){},
            methods:{}
          }
          </script>

          關(guān)于遞歸組件的內(nèi)容,在官方文檔里是有提到的-->遞歸組件

          使用render方法

          除了使用組件的方式,也可以使用vue的render方法,去利用JavaScript 的完全編程的能力,實現(xiàn)遞歸處理樹形數(shù)據(jù),從而展示出無限層級樹。如下:

          <template>
            <tree-comp :treeData="mainData"></tree-comp>
          </template>
          <script>
          export default {
            name: 'treeRender',
            data () {
              return {
                mainData: {
                  value: "root",
                  children:[
                    {
                      value: "層級1-1",
                      children:[{
                        value: "層級2-1",
                        children:[{
                          value: "層級3-1",
                          children:[]
                        }]
                      },{
                        value: "層級2-2",
                        children:[]
                      }]
                    },{
                      value: "層級1-2",
                      children:[]
                    }
                  ]
                }
              }
            },
            components:{
              treeComp:{
                functional: true,
                props: {treeData: Object},
                render(h, {props: {treeData = {}}}) {
                  const creatNode = (node)=>{
                    if(node.children && node.children.length > 0){
                      let hArr = node.children.map(item=>{
                        return creatNode(item)
                      })
                      return h('div', {class:'demo'}, [node.value, hArr])
                    }else{
                      return h('div', {class:'demo'}, [node.value])
                    }          
                  }
                  return creatNode(treeData)
                },
              }
            },
            mounted(){},
            methods:{}
          }
          </script>
          <style lang="less" scoped>
            .demo{padding:5px 0;margin:1px 10px;text-align: left;font-size:16px;max-width:500px;border-left:1px dashed #999;
              &:before{content:'--';display: inline-block;padding:0 4px;}
            }
          </style>

          其中最核心的就是在render方法里,creatNode方法用遞歸的方式,深度優(yōu)先遍歷樹狀數(shù)據(jù),生成vnode,然后渲染出了頁面。



          點擊左下角閱讀原文,到 SegmentFault 思否社區(qū) 和文章作者展開更多互動和交流,掃描下方”二維碼“或在“公眾號后臺回復(fù)“ 入群 ”即可加入我們的技術(shù)交流群,收獲更多的技術(shù)文章~

          - END -



          瀏覽 65
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日日爽,夜夜爽,天天爽 | 看操年轻小媳妇逼毛片视频 | 丁香婷婷五月激情综合 | 九九亚洲视频 | 亚洲网站在线观看视频 |