vue 组件递归

组件递归常用到的栗子就比如树形结构的创建,需要自调用进行递归渲染
下面是递归组件渲染tree的效果图:

效果.jpeg

完整的代码仓库地址:https://github.com/HuangPingP/vue-tree
1、创建一个treeMenu.vue的组件文件,组件内调用必须定义name 才能自调用

  <ul class="item">
    <li >
      <div :class="{bold: isFolder,'item-info':true}" @click="toggle" >
        <!-- 伸缩图标 -->
        <span 
        v-if="isFolder" 
        :class="{'iconfont':true,'icon-right-allow':!open,'icon-down-allow':open}">
        </span>
        <span class="item-name ellipsis">{{ model.name }}</span>

        <!-- 操作栏 -->
        <p class="edit-box">
          <i class="iconfont icon-add" @click="addChild"></i>
          <i class="iconfont icon-edit"></i>
          <i class="iconfont icon-dele"></i>
        </p>
      </div>

      <!-- 组件递归调用 -->
      <ul class="item-child" v-show="open" v-if="isFolder">
        <item 
          v-for="(model, index) in model.children" 
          :key="index" 
          :model="model">
        </item>
      </ul>
    </li>
  </ul>
  import Vue from 'vue'
  export default {
    props: ['model'],
    name: 'item',  // 必须给组件命名
    data() {
      return {
         open: true
      }
    },
    computed: {
    isFolder() {
      return this.model.children &&
        this.model.children.length
    }
  },
  methods: {
    toggle() {
      if (this.isFolder) {
        this.open = !this.open
      }
    },
    addChild() {
      if (!this.isFolder) {
        Vue.set(this.model, 'children', [])
        this.open = true
      }
      this.model.children.push({
        name: 'new stuff'
      })
    }
  }
}

2.调用组件 在helloWord.vue中调用 treeMenu.vue组件

  <tree-menu :model="treeData"></tree-menu>
//数据结构
        treeData:{
            name: 'My Tree',
            children: [
                { name: 'hello' },
                { name: 'watdfs' },
                {
                name: 'child folder',
                children: [
                    {
                    name: 'child folder',
                    children: [
                        { name: 'hello' },
                        { name: 'wat' }
                    ]
                    },
                    { name: 'hello' },
                    { name: 'wat' },
                    {
                    name: 'child folder',
                    children: [
                        { name: 'hello' },
                        { name: 'wat' }
                    ]
                    }
                ]
                }
            ]
        }

参考https://cn.vuejs.org/v2/examples/tree-view.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 管理系统中很多这样树形菜单显示的,这里要用到组件的递归来完成,这里我们来学习下vue关于组件递归的实现。 ...
    席坤阅读 1,090评论 2 4
  • 最近做项目经常会用到一些UI库,比如element、iview等,这些能够快速构建应用的库真的十分方便。比如ivi...
    ALOLONGHUN阅读 17,551评论 5 15
  • 写在前面有时候我们需要树状菜单,比如通讯录的分组列表。在这时候数据是树状存储的,给前端的数据需要进行递归读取。这时...
    Yuhoo阅读 280评论 0 0
  • 基于Vue的组件库 https://github.com/ElemeFE/element" element 饿了么...
    技术小白熊阅读 1,745评论 0 16
  • 基于Vue的组件库 https://github.com/ElemeFE/element" element 饿了么...
    不和谐发光体阅读 1,174评论 0 8