HarmonyOS 报错Cannot find name 'backgroundColor'. Did you mean the instance member 'this.backgroundColor'?

自定义的CardView想在调用的地方设置backgroundColor等属性值,无法设置报以上错误。

// 自定的CardView
@Component
export struct CardView {
  @BuilderParam private content: () => void // 控件占位符

  build() {
    Column() {
      this.content()
    }
    .shadow({
      radius: 6,
      color: Color.Grey,
      offsetX: 0,
      offsetY: 0
    })
    .padding({
      left: 30,
      right: 30,
      top: 20,
      bottom: 20
    })
    .borderRadius(6)
    .alignItems(HorizontalAlign.Start)
  }
}

// 调用CardView
CardView() {
  Row() {
  }.width("100%")
}
.backgroundColor(Color.White) // 这里报错:Cannot find name 'backgroundColor'. Did you mean the instance member 'this.backgroundColor'?
  • 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.
HarmonyOS
2025-01-09 16:03:48
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考示例如下:

@Entry
@Component
struct Page {
  @Builder
  test() {
    Text('this is test11')
  }

  build() {
    Stack() {
      CardView({
        content: this.test
      })
        .backgroundColor(Color.White)
    }
  }
}

@Component
struct CardView {
  @Builder
  blank() {
  }

  @BuilderParam content: () => void = this.blank

  build() {
    Column() {
      this.content()
    }.shadow({
      radius: 6,
      color: Color.Grey,
      offsetX: 0,
      offsetY: 0
    })
    .padding({
      left: 30,
      right: 30,
      top: 20,
      bottom: 20
    })
    .borderRadius(6)
    .alignItems(HorizontalAlign.Start)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:30:39
相关问题
HarmonyOS Cannot find name 'ESObject'
768浏览 • 0回复 待解决
Span设置backGroundColor不生效是为什么?
1073浏览 • 1回复 待解决