HarmonyOS export struct的初始化参数?

@Preview
@Component
export struct ContentItemView {
  private itemInfo: ContentListItemInfo
  constructor(itemInfo: ContentListItemInfo) {
    super()
    this.itemInfo = itemInfo
  }

  build() {
  }
}

class ContentListItemInfo {

在上述代码中,itemInfo会报错,因为没有初始化。

我的目的是,写一个布局,通过Info的参数进行数据配置。那么这个 itemInfo理论上会传递进来,要怎么改?

阅读 544
1 个回答

private 私有变量不能接受外部传进来的参数,传进来的参数需要通过prop去接收。

请参考示例:

//index.ets
import { ContentItemView,ContentListItemInfo } from '../pages/Page2'
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private obj: ContentListItemInfo = {
    value: '哈哈哈哈',
    flag: false
  }

  build() {
    RelativeContainer() {
      ContentItemView({ itemInfo: this.obj })


    }
    .height('100%')
    .width('100%')
  }
}
// page2.ets
export class ContentListItemInfo {
  value: string = ''
  flag: boolean = false
}

@Entry
@Component
export struct ContentItemView {
  itemInfo: ContentListItemInfo = new ContentListItemInfo()

  build() {
    Text(this.itemInfo.value)
      .backgroundColor(Color.Red)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进