HarmonyOS 鸿蒙Next中自定义组件能否递归嵌套?

HarmonyOS 鸿蒙Next中自定义组件能否递归嵌套? 试了一下下面文章的方法没成功

https://blog.csdn.net/ldc121xy716/article/details/150280579

cke_2429.png

interface TreeNode {
  id: number;
  name: string;
  children?: TreeNode[];
}

@Entry
@Component
struct TreeNodeComp {
  @State data: TreeNode[] = [
    {
      id: 1,
      name: '前端技术',
      children: [
        {
          id: 2,
          name: 'JavaScript',
          children: [
            { id: 3, name: 'ES6 特性' },
            { id: 4, name: '异步编程' }
          ]
        },
        {
          id: 5,
          name: 'Vue.js',
          children: [
            { id: 6, name: 'Vue 3 新特性' },
            {
              id: 7,
              name: '高级用法',
              children: [
                { id: 8, name: '递归组件' },
                { id: 9, name: '渲染函数' }
              ]
            }
          ]
        }
      ]
    },
    {
      id: 10,
      name: '后端技术',
      children: [
        { id: 11, name: 'Node.js' },
        { id: 12, name: 'Python' }
      ]
    }
  ];

  build() {
    Column() {
      ForEach(this.data, (node: TreeNode) => {
        TreeNodeComponent({ node: node });
      });
    }
    .width('100%')
    .height('100%')
    .padding(20)
    .backgroundColor('#ffffff');
  }
}

@Component
struct TreeNodeComponent {
  @Prop node: TreeNode;
  @Prop expand: boolean = false;

  build() {
    Column() {
      Row({ space: 5 }) {
        if (this.node.children && this.node.children.length > 0) {
          Image(this.expand ? $r('app.media.startIcon') : $r('app.media.startIcon'))
            .width(20)
            .height(20)
            .onClick(() => {
              this.expand = !this.expand;
            });
        } else {
          Image($r('app.media.startIcon'))
            .width(20)
            .height(20);
        }

        Text(this.node.name)
          .fontSize(16)
          .fontWeight(500)
          .layoutWeight(1);
      }
      .width('100%')
      .padding({
        left: 10,
        right: 10,
        top: 5,
        bottom: 5
      })
      .backgroundColor('#f5f5f5')
      .borderRadius(5)
      .margin({ bottom: 5 });

      if (this.node.children && this.node.children.length > 0 && this.expand) {
        ForEach(this.node.children, (childNode: TreeNode) => {
          TreeNodeComponent({ node: childNode });
        });
      }
    }
    .width('100%')
    .padding({ left: 20 });
  }
}

更多关于HarmonyOS 鸿蒙Next中自定义组件能否递归嵌套?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

楼主,代码实际是已经完成子组件嵌套了。 需要运行后点击左侧图标展开子组件,你再试一下。

更多关于HarmonyOS 鸿蒙Next中自定义组件能否递归嵌套?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


😄何等的师太!!!,

在HarmonyOS鸿蒙Next中,自定义组件支持递归嵌套。通过组件自身的标识在布局中引用自己,可实现递归渲染。需注意设置终止条件,例如使用@State变量控制嵌套深度,防止无限递归导致栈溢出。递归嵌套适用于树形结构等动态层级数据的场景。

在HarmonyOS Next中,自定义组件支持递归嵌套,但需要正确使用条件渲染和状态管理。从你的代码看,问题可能出现在以下几个方面:

  1. 组件引用方式:在TreeNodeComp中调用TreeNodeComponent时,应该使用组件标签语法而非函数调用:
ForEach(this.data, (node: TreeNode) => {
  TreeNodeComponent({ node: node })  // 错误方式
})

应改为:

ForEach(this.data, (node: TreeNode) => {
  TreeNodeComponent({ node: node })  // 正确方式
})
  1. 状态管理:递归组件中的expand状态应该使用@State装饰器,确保状态变化能触发UI更新:
[@State](/user/State) expand: boolean = false;
  1. 递归终止条件:你的代码已经正确通过children数组长度判断来终止递归,这是正确的做法。

  2. 资源引用:确保$r(‘app.media.startIcon’)引用的图片资源存在且路径正确。

建议检查以上几点,特别是组件调用语法和状态管理部分。递归组件在HarmonyOS Next中是完全可行的,常用于树形结构、嵌套菜单等场景。

回到顶部