鸿蒙学习记录

150 阅读1分钟

写法

ArkTs

// Second.ets
@Entry
@Component
struct Second {
  @State message: string = 'Hi there'

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button() {
          Text('Back')
            .fontSize(25)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#0D9FFB')
        .width('40%')
        .height('5%')
      }
      .width('100%')
    }
    .height('100%')
  }
}

路由

路由注册

默认路由文件:entry > src > main > resources > base > profilemain_pages.json文件中src数组中注册路由

路由操作

跳转

router.pushUrl({ url: 'pages/Second' })

返回

router.back()

运行

点击File > Project Structure...  > Project > SigningConfigs界面勾选“Support HarmonyOS”和“Automatically generate signature”,点击界面提示的“Sign In”,使用华为账号登录。等待自动签名完成后,点击“OK“即可。

生命周期

应用

onCreate

onForeground

onBackground

onDestroy

页面

onPageShow

onPageHide

onBackPress

修饰器

@State

相当于React中的useState(),去申明一个动态的数据。

@Props

用于接收父组件传递的数据并声明

@Link

定义一个不可初始化的值,作用为父子组件数据的双向同步