HarmonyOS export struct的初始化参数?

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

  build() {
  }
}

class ContentListItemInfo {

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

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

阅读 543
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)
  }
}