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")
    }
  }
}

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%')
  }
}

同一个库引入

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")
    }
  }
}

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%')
  }
}

你可能感兴趣的:(#,ArkTS,前端,开发语言)