当前位置: 首页 > news >正文

公司都是自己制作网站国家免费职业技能培训官网

公司都是自己制作网站,国家免费职业技能培训官网,网站建设 教材 推荐,静态网站 站内搜索自定义组件: 概念: 由框架直接提供的称为 系统组件, 由开发者定义的称为 自定义组件。 源代码: Component struct MyCom { build() { Column() { Text(我是一个自定义组件) } } } Component struct MyHeader { build() { Row(…

自定义组件:
概念: 由框架直接提供的称为 系统组件, 由开发者定义的称为 自定义组件。
源代码:

@Component
struct MyCom {
  build() {
    Column() {
      Text('我是一个自定义组件')
    }
  }
}

@Component
struct MyHeader {
  build() {
    Row() {
      Text('我是头部')
        .fontColor(Color.White)
    }
    .width('100%')
    .height(50)
    .backgroundColor(Color.Brown)
  }
}

// 快捷点 comp
@Component
struct MyMain{
  build() {
    Column() {
      MyCom()
      MyCom()
    }
    // .height(400)
    .layoutWeight(1)
    .width('100%')
    .backgroundColor(Color.Gray)
  }
}

@Component
struct MyFooter {
  build() {
    Row() {
      Text('我是底部')
        .fontColor(Color.White)
    }
    .width('100%')
    .height(50)
    .backgroundColor(Color.Green)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyHeader()
      MyMain()
      MyFooter()
    }
  }
}

自定义组件-通用属性和方法
// 尽量使用按需导出
@priview // 自定义view的预览
@Component
export struct 组件名{}

自定义组件---成员函数变量
除了必须要实现 build() 函数外,还可以定义其它的成员函数,以及成员变量
成员变量的值 -> 外部可传参覆盖  也就是带=号的变量及其方法都可以被覆盖
源代码:
@Component
struct MyPanel {
  // 成员变量
  title: string = '默认的大标题'
  extra: string = '查看更多 >'
  // 成员变量 - 函数 - 可以外部传入覆盖的
  getMore = () => {
    AlertDialog.show({
      message: '查看更多'
    })
  }

  // 成员函数 - 不可以外部传入覆盖 【没有等号】
  sayHi() {
    AlertDialog.show({
      message: '你好'
    })
  }

  build() {
    Column() {
      Row() {
        Text(this.title).fontSize(18)
        Text(this.extra).fontSize(18)
          .onClick(() => {
            this.getMore()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceBetween)
      Row() {
        Text('内容部分').fontSize(18)
      }
      .padding(20)
    }
    .width('100%')
    .height(200)
    .padding(10)
    .margin({bottom: 20})
    .borderRadius(10)
    .backgroundColor(Color.White)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyPanel({
        title: '我的订单',
        extra: '全部订单 >',
        getMore() {
          AlertDialog.show({
            message: '查看全部订单'
          })
        }
      })
      MyPanel({
        title: '小米有品众筹',
        extra: '7款众筹中 >',
        getMore() {
          AlertDialog.show({
            message: '查看7款众筹'
          })
        }
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#ccc')
    .padding(20)
  }
}

@BuilderParam 传递UI

利用 @BuilderParam 构建函数,可以让自定义组件 允许外部传递UI。


@Component
struct SonCom {
  // 1、定义构建函数
  @BuilderParam ContentBuilder: () => void = this.defaultBuilder
  @Builder
  defaultBuilder() {
    Text('默认的内容')
      .fontColor('#000')
  }

  build() {
    // 2、使用构建函数,构建结构
    Column() {
      this.ContentBuilder()
    }
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      // SonCom()
      SonCom() {
        Button('跳转')  // 则显示这个button,否则显示上面默认的内容
      }
    }
  }
}

完善的源代码:
@Component
@Preview
struct MyPanel {
  // 成员变量
  title: string = '默认的大标题'
  extra: string = '查看更多 >'
  // 成员变量 - 函数 - 可以外部传入覆盖的
  getMore = () => {
    AlertDialog.show({
      message: '查看更多'
    })
  }

  // 成员函数 - 不可以外部传入覆盖 【没有等号】
  sayHi() {
    AlertDialog.show({
      message: '你好'
    })
  }

  @BuilderParam ContentBuilder: () => void = this.defaultBuilder
  @Builder
  defaultBuilder() {Text('默认文本')}

  build() {
    Column() {
      Row() {
        Text(this.title).fontSize(18)
        Text(this.extra).fontSize(18)
          .onClick(() => {
            this.getMore()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.SpaceBetween)
      Row() {
        // Text('内容部分').fontSize(18)
        // 这里的结构不希望写死,需要通过 BuilderParams 来进行构建
        this.ContentBuilder()
      }
      .padding(20)
    }
    .width('100%')
    .height(200)
    .padding(10)
    .margin({bottom: 20})
    .borderRadius(10)
    .backgroundColor(Color.White)
  }
}

@Entry
@Component
struct Index {
  build() {
    Column() {
      MyPanel({
        title: '我的订单',
        extra: '全部订单 >',
        getMore() {
          AlertDialog.show({
            message: '查看全部订单'
          })
        }
      }) {
        Text('我是订单 - 相关的文本')
      }
      MyPanel({
        title: '小米有品众筹',
        extra: '7款众筹中 >',
        getMore() {
          AlertDialog.show({
            message: '查看7款众筹'
          })
        }
      }) {
        Button('我是小米有品众筹的按钮')
      }
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#ccc')
    .padding(20)
  }
}

效果图:

多个 @BuilderParam 参数
子组件有多个BuilderParam, 必须通过参数的方式来传入

@Component
@Preview
struct MyCard {@BuilderParam titleBuilder: () => void = this.titleDefaultBuilder@BuildertitleDefaultBuilder() {Text('我是默认的大标题')}@BuilderParam contentBuilder: () => void = this.contentDefaultBuilder@BuildercontentDefaultBuilder() {Text('我是默认的内容')}build() {// 卡片组件Column() {// 标题部分Row() {this.titleBuilder()}.height(30).width('100%').border({color: '#ccc', width: {bottom: 1}}).padding({left: 10})// 内容部分Row() {this.contentBuilder()}.width('100%').padding(10)}.width('100%').height(100).borderRadius(10).backgroundColor(Color.White).justifyContent(FlexAlign.Start)}
}@Entry
@Component
struct Index {@Builder ftBuilder() {Text('我是传入的大标题结构')}@Builder fcBuilder() {Text('我是传入的大标题结构')}build() {Column({space: 10}) {// 希望标题部分和内容部分是自定义的MyCard({titleBuilder: this.ftBuilder,contentBuilder: this.fcBuilder})MyCard({titleBuilder: this.ftBuilder,contentBuilder: this.fcBuilder}) // {} 尾随闭包是不可以}.width('100%').height('100%').padding(20).backgroundColor('#ccc')}
}


 

http://www.yayakq.cn/news/976610/

相关文章:

  • 网站怎么做长尾关键词无锡网站优化哪家快
  • 阿里云虚拟主机建网站样品门展厅设计图片
  • 用wordpress做什么内容沈阳网络优化培训
  • 中天建设中瑞物资网站展示网站报价
  • 建设网站用什么空间服务器asp.net怎么做网站
  • 环宇网站建设网络规划设计师通过率最低
  • 鲅鱼圈做网站网工资页多少钱一个月国外好看的教育类网站模板下载
  • 建一个门户网站旅游网站建设费用
  • 网上帮别人做网站wordpress 三站合一
  • 微信公众号怎么做的跟网站似的孙俪做的网站广告
  • 仪征做网站公司电商网站建设题库
  • 网站邮箱建设网站新闻专题怎么做
  • 无经验做网站呼和浩特电子商务网站建设
  • 平台网站建设ppt模板下载哪个网站可以做创意短视频网站
  • 教育网站 php重庆代还信用卡网站建设
  • 三网合一网站报价html网页爱心代码
  • 智能做网站百度seo关键词优化
  • 网站制作公司的流程寻找常州微信网站建设
  • 广东网站建设十大品牌永久免费影视建站程序
  • 做图素材网站开通会员哪个好行业网站运营方案
  • 视频网站开发phpwordpress 添加评论等级
  • 企业网站的缺点建网站方法
  • 在自己的网站上怎么做淘宝客网站精神文件建设专栏
  • 在线教育类网站模板企业网站货物查询怎么做
  • 网络教育网站建设如何搭建自己的网站平台
  • 青海网站建设 小程序开发购门网站建设
  • 花都营销型网站电影打卡WordPress模板
  • 西海岸城市建设局网站网站名称有哪些
  • 建立专业的官方网站设计软件网站定制开发
  • seo网站推广首页排名wordpress apache iis