VUE2.0——树形结构数据(递归组件)

数据结构

有不知道多少和多深的child
在这里插入图片描述

父组件

newstree组件是一个循环递归的子组件
这里有一个itemChild是数据结构需要递归的数据
deep这个是递归的深度
ref是父组件触发子组件所用
我这里可能有不同的层级的子组件所以不是单一的所以是个动态的

              <div class="replay_child">
                <newstree
                  :itemChild="item.child"
                  v-if="item.child != false"
                  :deep="deep"
                  :ref="`child${item.id}`"
                  :key="'father' + item.id"
                  @getcommentid="getcommentid"
                ></newstree>
              </div>

子组件

这里的关键还是子组件循环调用自己
值得注意的地方就是需要加 name: “comment-tree”,不然无法区分是调用的自己

关键的代码:

      <comment-tree
        v-if="itemChild"
        :itemChild="item.child"
        :deep="deep + 1"
        :key="'son' + item.id"
        @getcommentid="reply"
        :showreply="flag"
      >
      </comment-tree>
<template>
  <div class="comment-wrap">
    <div
      v-for="(item, index) in child"
      :key="index"
      :style="{
        'margin-left': deep >= 74 ? '0px' : '0.1rem',
        'word-wrap': 'break-word',
      }"
      class="replay"
      v-if="flag || showreply"
    >
      <div
        :style="{
          width: 9 - deep * 0.1 + 'rem',
          'word-wrap': 'break-word',
        }"
      >
        {{ item.user_name }} : {{ item.c_body }}
        <span @click="reply(item)" class="reply_span cu">回复</span>
      </div>

      <comment-tree
        v-if="itemChild"
        :itemChild="item.child"
        :deep="deep + 1"
        :key="'son' + item.id"
        @getcommentid="reply"
        :showreply="flag"
      >
      </comment-tree>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      child: "",
      flag: false,
    };
  },
  name: "comment-tree",
  props: ["itemChild", "deep", "showreply"],
  methods: {
    reply(item) {
      this.$emit("getcommentid", item);
    },
    reply1(item) {
      this.$emit("getcommentid", item);
    },
    changeshow() {
      this.flag = !this.flag;
    },
  },
  computed: {
    itemChildfn() {
      if (this.itemChild != false) {
        return this.itemChild;
      }
    },
  },
  watch: {
    itemChildfn(newValue) {
      this.child = newValue;
    },
  },
  created() {
    this.child = this.itemChild;
    this.flag = this.showreply;
  },
};
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 2.0 提供了一个递归组件 `a-tree`,可以用来实现递归菜单。 首先,需要在组件中引入 `a-tree` 组件: ```vue <template> <a-tree :tree-data="treeData"> <template v-slot="{data}"> <span>{{ data.title }}</span> </template> </a-tree> </template> <script> import { ATree } from 'ant-design-vue' export default { name: 'RecursiveMenu', components: { ATree }, data() { return { treeData: [ { title: 'Menu 1', key: '1', children: [ { title: 'Submenu 1-1', key: '1-1', children: [ { title: 'Sub-submenu 1-1-1', key: '1-1-1' }, { title: 'Sub-submenu 1-1-2', key: '1-1-2' } ] }, { title: 'Submenu 1-2', key: '1-2' } ] }, { title: 'Menu 2', key: '2', children: [ { title: 'Submenu 2-1', key: '2-1' }, { title: 'Submenu 2-2', key: '2-2' } ] } ] } } } </script> ``` 在 `treeData` 中定义了一个递归菜单的数据结构,其中每个菜单项都有一个 `title` 和一个 `key`,以及可能有 `children`,用于定义子菜单。在模板中,使用 `a-tree` 组件来渲染递归菜单,并使用插槽来定义每个菜单项的渲染方式。 在 `a-tree` 组件中,使用 `tree-data` 属性来传递菜单数据。在插槽中,使用 `data` 对象来获取当前菜单项的数据,其中包含菜单项的 `title`、`key`、`children` 等信息。 通过这种方式,就可以实现一个简单的递归菜单。需要注意的是,`a-tree` 组件本身提供了许多属性和事件,可以用来控制菜单的交互方式,例如菜单的展开、收起、选中等。具体使用方式可以参考 Ant Design Vue 2.0 官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值