当前位置:   article > 正文

HarmonyOS 组件9--自定义组件_harmoney os export 组件

harmoney os export 组件

定义自定义组件

有时需要在不同的页面写一些相同或者类似的组件代码,如果可以定义为一个组件来使用的话,那会对开发来说方便不少。
语法:

  1. // 定义自定义组件
  2. @Component
  3. struct Header{
  4. private title :string
  5. build(){
  6. Row(){
  7. Image($r('app.media.back'))
  8. .width(30)
  9. Text(this.title)
  10. .fontSize(30)
  11. .fontWeight(FontWeight.Bold)
  12. Blank() // 插入空白组件
  13. Image($r('app.media.refresh'))
  14. .width(30)
  15. }
  16. .width('100%')
  17. .height(50)
  18. }
  19. }
  20. // ...
  21. // 使用时
  22. Header({title:'Hello World'})

将所有的组件添加到一个文件夹中

  1. 新建文件夹
  2. 创建一个ArkTS文件来编写自定义组件
  3. 将写好的组件复制到ArkTS文件中

    别忘了export导出模块,给外部使用
  4. 在外部引用
    import {Header} from '../components/herder'

全局自定义构建函数

  1. // 全局自定义构建函数
  2. @Builder function ItemCard(item:Item){
  3. Row(){
  4. Column(){
  5. Text(item.name)
  6. Text(item.price)
  7. }
  8. .alignItems(HorizontalAlign.Start) // 左对齐
  9. .width(300)
  10. }
  11. }

设置一个卡片生成函数,之后在ForEach循环函数中调用即可,将冗长的代码进行封装。

  1. ForEach(this.item,(item:Item)=>{
  2. ItemCard(item) // 列表卡片自定义函数
  3. })

局部自定义构建函数

  1. // 局部自定义构建函数
  2. @Builder ItemCard(item:Item){ // 不用写fuction了
  3. Row(){
  4. Column(){
  5. Text(item.name)
  6. Text(item.price)
  7. }
  8. .alignItems(HorizontalAlign.Start) // 左对齐
  9. .width(300)
  10. }
  11. }

整体代码

  1. import router from '@ohos.router'
  2. import {Header} from '../components/herder'
  3. // 声明一个内部类Item
  4. class Item{
  5. name:string
  6. price:string
  7. discount:number
  8. // 构造函数
  9. constructor(name:string,price:string,discount:number = 0) {
  10. this.name = name
  11. this.price = price
  12. this.discount = discount
  13. }
  14. }
  15. /*
  16. // 全局自定义构建函数
  17. @Builder function ItemCard(item:Item){
  18. Row(){
  19. Column(){
  20. Text(item.name)
  21. Text(item.price)
  22. }
  23. .alignItems(HorizontalAlign.Start) // 左对齐
  24. .width(300)
  25. }
  26. }
  27. */
  28. @Entry
  29. @Component
  30. struct Index {
  31. @State message1: string = 'Harmony'
  32. @State message2: string = '遥遥领先!'
  33. @State imageWidth : number = 200
  34. private item:Array<Item> = [ // 创建对象
  35. new Item('华为','1000$'),
  36. new Item('菠萝','2000$'),
  37. new Item('小米','3000$'),
  38. new Item('荣耀','4000$'),
  39. new Item('聯想','5000$'),
  40. new Item('Vivo','6000$'),
  41. new Item('苹果','7000$')
  42. ]
  43. build() {
  44. Row() {
  45. Column() {
  46. Header({title:'Hello World'})
  47. Image($r('app.media.image'))
  48. .width(this.imageWidth)
  49. .height(200)
  50. .borderRadius(20)
  51. ForEach(this.item,(item:Item)=>{
  52. this.ItemCard(item) // 列表卡片自定义函数 使用this就不会报错了
  53. })
  54. }
  55. .justifyContent(FlexAlign.Start)
  56. .width('100%')
  57. .height('100%')
  58. }
  59. .height('100%')
  60. }
  61. // 局部自定义构建函数
  62. @Builder ItemCard(item:Item){ // 不用写fuction了
  63. Row(){
  64. Column(){
  65. Text(item.name)
  66. Text(item.price)
  67. }
  68. .alignItems(HorizontalAlign.Start) // 左对齐
  69. .width(300)
  70. }
  71. }
  72. }

设置全局公共样式封装函数

  1. // 全局公共样式
  2. @Styles function fillScreen() {
  3. .width('100%')
  4. .height('100%')
  5. }
  6. // ......
  7. Row(){
  8. // ...
  9. }
  10. .fillScreen() // 设置全局样式

如果属性不是公共使用的属性,而是文字的字体大小等属性怎么办?

设置继承封装样式

  1. @Extend(Text)function priceText(){
  2. .fontWeight(FontWeight.Bold)
  3. .fontSize(20)
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号