当前位置:   article > 正文

ArkTS-自定义component引入

ArkTS-自定义component引入

除了pages外的components引入

src/main/ets/MainAbility/components/compTest.ets

@Entry
@Component
export struct compTest{
  @State one:string = "Test"
  build(){
    Column(){
      Text(this.one).fontSize(25).fontColor("#f0f")
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

src/main/ets/MainAbility/pages/index.ets

import { compTest } from '../components/compTest'; //引入
import router from '@system.router'
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        compTest()//调用
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

同一个库引入

src/main/ets/MainAbility/components/compTest.ets

@Entry
@Component
export struct compTest{
  @State one:string = "Test"
  build(){
    Column(){
      Text(this.one).fontSize(25).fontColor("#f0f")
    }
  }
}

@Component
export struct compTest01{
  @State two:string = "Test01"
  build(){
    Column(){
      Text(this.two).fontSize(30).fontColor("#f00")
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

src/main/ets/MainAbility/pages/index.ets

import { compTest,compTest01 } from '../components/compTest';
import router from '@system.router'
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        compTest()
        compTest01()
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/294927
推荐阅读
相关标签
  

闽ICP备14008679号