HarmonyOS 自定义组件构造了ListItemGroup,但是在List组件中应用时被校验出不属于List子组件类型

自定义组件构造了ListItemGroup,再在页面中导入该自定义组件,并在List组件中应用时被校验出不属于List子组件类型,报错:

xxx component cannot be a child component of the List component.<ArkTSCheck>
  • 1.
HarmonyOS
2024-12-26 15:17:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

demo如下:

ListPage.ets:

import TimeTable from './TimeTable'
import ListGroupItemComponent from './ListGroupItemComponent'

@Entry
@Component
struct ListPage {
  private timeTable: TimeTable[] = [
    {
      title: '星期一',
      projects: ['语文', '数学', '英语']
    },
    {
      title: '星期二',
      projects: ['物理', '化学', '生物']
    },
    {
      title: '星期三',
      projects: ['历史', '地理', '政治']
    },
    {
      title: '星期四',
      projects: ['美术', '音乐', '体育']
    }
  ]

  build() {
    Column() {
      List({ space: 20 }) {
        ForEach(this.timeTable, (item: TimeTable) => {
          ListGroupItemComponent({item: item});
        })
      }
      .width('90%')
      .sticky(StickyStyle.Header | StickyStyle.Footer)
      .scrollBar(BarState.Off)
    }.width('100%').height('100%').backgroundColor(0xDCDCDC).padding({ top: 5 })
  }
}


TimeTable.ets:

export default interface TimeTable {
  title: string;
  projects: string[];
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

ListGroupItemComponent .ets:

import TimeTable from './TimeTable'

@Component
export default struct ListGroupItemComponent {
  @Prop item: TimeTable;

  build() {
    ListItemGroup({ header: this.itemHead(this.item.title), footer: this.itemFoot(this.item.projects.length) }) {
      ForEach(this.item.projects, (project: string) => {
        ListItem() {
          Text(project)
            .width("100%")
            .height(100)
            .fontSize(20)
            .textAlign(TextAlign.Center)
            .backgroundColor(0xFFFFFF)
        }
      }, (item: string) => item)
    }
    .divider({ strokeWidth: 1, color: Color.Blue }) // 每行之间的分界线

  }

  @Builder
  itemHead(text: string) {
    Text(text)
      .fontSize(20)
      .backgroundColor(0xAABBCC)
      .width("100%")
      .padding(10)
  }

  @Builder
  itemFoot(num: number) {
    Text('共' + num + "节课")
      .fontSize(16)
      .backgroundColor(0xAABBCC)
      .width("100%")
      .padding(5)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
分享
微博
QQ
微信
回复
2024-12-26 17:44:04


相关问题
HarmonyOS list控件组件复用
1206浏览 • 1回复 待解决
List组件divider颜色显示透List组件颜色
789浏览 • 0回复 待解决
HarmonyOS 定义自定义组件
1036浏览 • 1回复 待解决