OpenHarmony开发者论坛

标题: xxx must have one and only one property decorated with @BuilderParam, and its @BuilderParam expects no parameter [打印本页]

作者: 兴旺    时间: 前天 16:02
标题: xxx must have one and only one property decorated with @BuilderParam, and its @BuilderParam expects no parameter
【问题描述】
(, 下载次数: 0)
报错信息:In the trailing lambda case, 'UserInfo' must have one and only one property decorated with @BuilderParam, and its @BuilderParam expects no parameter

  1. /**
  2. * 14.学习自定义组件
  3. */
  4. import toast from '../utils/Utils'
  5. import { UserInfo } from './components/MyComponents'

  6. @Builder
  7. function Avatar() {
  8.   Row() {
  9.     Text("头像:")
  10.     Image($r("app.media.avatar"))
  11.       .fitOriginalSize(true)
  12.       .height(50)
  13.   }.backgroundColor(Color.Pink)
  14. }


  15. @Entry
  16. @Component
  17. struct Learn13CustomComponent {
  18.   build() {
  19.     Column({ space: 10 }) {
  20.       UserInfo({ name: "崔兴旺" }).backgroundColor(Color.Orange).width(300).height(150)
  21.       UserInfo({
  22.         name: "张三", say: (_name: string) => {
  23.           toast(`${_name}说:hello`)
  24.         },
  25.       }){
  26.         Avatar()
  27.       }

  28.       UserInfo({ name: "李四" })
  29.     }
  30.   }
  31. }

复制代码
子组件代码:
  1. import toast from "../../utils/Utils"

  2. /**
  3. * 这是插槽中默认的内容
  4. */
  5. @Builder
  6. function defaultContent() {
  7.   Text("我是默认内容")
  8. }


  9. @Component
  10. export struct UserInfo {
  11.   @State name: string = ""
  12.   @State age: number = 0
  13.   say = (_name: string) => {
  14.     toast(`${_name}说:你好`)
  15.   }

  16.   @Builder
  17.   defaultContent2() {
  18.     Text("我是默认内容2")
  19.   }

  20.   // @Require//父组件必选传给本子组件
  21.   @BuilderParam
  22.   Content: () => void = defaultContent
  23.   @BuilderParam
  24.   Content2: () => void = this.defaultContent2

  25.   build() {
  26.     Column() {
  27.       Text(this.name).fontSize(20).backgroundColor(Color.Red)
  28.       Text(this.age.toString()).fontSize(20).backgroundColor(Color.Blue)
  29.       this.Content()
  30.       this.Content2()
  31.     }.backgroundColor(Color.Green).width(190).height(140).onClick(() => {
  32.       this.say(this.name)
  33.     })
  34.   }
  35. }

复制代码

(, 下载次数: 0)

【运行环境】
(, 下载次数: 0)
SDK版本:"compatibleSdkVersion": "5.0.0(12)"

作者: fengyunrenwu    时间: 7 小时前
@BuilderParam定义了两个,但是闭包场景只能一个哈
作者: fengyunrenwu    时间: 7 小时前
参考下这个:https://gitee.com/openharmony/do ... 6%E7%BB%84%E4%BB%B6




欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5