ArkUI框架,Flex布局,基础组件及封装,父子组件的双向绑定

ArkUI框架,Flex布局,基础组件及封装,父子组件的双向绑定

1.Flex布局初探

Flex组件用于创建弹性布局,开发者可以通过Flex的接口创建容器组件,进而对容器内的其他元素进行弹性布局,例如:使三个元素在容器内水平居中,垂直等间隔分散。

例如:如下的布局代码分别表示:(垂直排列,水平居中,垂直居中)

direction:FlexDirection.Column,
justifyContent:FlexAlign.Center,
alignItems:ItemAlign.Center

配合Text组件和Button组件,我们可以写一个基本的登录界面:

@Entry
@Component
struct Index {
  build() {
    Flex({ justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Center,
      direction: FlexDirection.Column }) {
      Text("登录")
        .fontSize(40)
        .fontWeight(700)
      TextInput()
        .width("80%")
        .height(40)
      TextInput()
        .width("80%")
        .height(40)
      Button("登录")
    }
    .width("100%")
    .height("100%")
    .border({ width: 10, color: "#000" })
  }
}

在这里插入图片描述


2.组件封装

我们可以将常用的组件进行自定义的封装,例如,我们想将输入框进行自定义的封装,以备后续进行调用:

自定义一个组件内容:(myInput.ets)

在这里插入图片描述

/* 定义子组件的内容 */
@Component
  /* struct用于定义组件的名称 */
struct MyInput {
  build() {
    TextInput()
      .width("80%")
      .height(40)
  }
}

export default MyInput

在主页面引用它:

import MyInput from "../common/components/myInput"
@Entry
@Component
struct Index {
  build() {
    Flex({ justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Center,
      direction: FlexDirection.Column }) {
      Text("登录")
        .fontSize(40)
        .fontWeight(700)
      MyInput()
      MyInput()
      Button("登录")
    }
    .width("100%")
    .height("100%")
    .border({ width: 10, color: "#000" })
  }
}

可以实现同上面一样的效果,但是组件的实现更加原子化:

在这里插入图片描述

我们也可以通过父组件向子组件传值,实现输入框的不同提示信息:

子组件新增Prop装饰器请求父组件传递的值:

@Component
  /* struct用于定义组件的名称 */
struct MyInput {
  @Prop placeholder:string

  build() {
    TextInput({ placeholder: this.placeholder })
      .width("80%")
      .height(40)
  }
}

export default MyInput

父组件在调用子组件时,向子组件传值:

import MyInput from "../common/components/myInput"
@Entry
@Component
struct Index {
  build() {
    Flex({ justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Center,
      direction: FlexDirection.Column }) {
      Text("登录")
        .fontSize(40)
        .fontWeight(700)
      MyInput({ placeholder:"输入用户名" })
      MyInput({ placeholder:"输入密码" })
      Button("登录")
    }
    .width("100%")
    .height("100%")
    .border({ width: 10, color: "#000" })
  }
}

效果如下:

在这里插入图片描述


3.设备管理器模拟

Previewer的页面无法进行实时的交互操作,所以我们要学会使用真机进行模拟

点击右上角如下选项:

在这里插入图片描述

选择华为P50,运行:

在这里插入图片描述

打开真机页面,运行:

在这里插入图片描述

模拟成功:

在这里插入图片描述


4.父子组件双向绑定

我们需要拿到用户输入的数据,以向后端发送一个登录请求,这就需要父子组件的双向绑定机制

要实现数据的双向绑定,我们先在父组件定义需要绑定的数据:

@State username: string = ""
@State password: string = ""

在输入框中添加inputValue参数:

MyInput({ placeholder: "输入用户名", inputValue: $username })
MyInput({ placeholder: "输入密码", inputValue: $password })

子组件中进行链接:

@Link inputValue: string

子组件中设置onChange事件,用于绑定数据的修改:

TextInput({ placeholder: this.placeholder, text: this.inputValue })
  .onChange((value:string) => {
    this.inputValue = value
  })
  .width("80%")
  .height(40)

随后,为了展示一个好的效果,我们还需要在登陆字的下面显示一下我们监听到的输入的数据内容:

/* 展示输入框输入的数据 */
Text(this.username)
  .fontSize(40)
  .fontWeight(700)
Text(this.password)
  .fontSize(40)
  .fontWeight(700)

整体代码如下:

index.ets:

import MyInput from "../common/components/myInput"

@Entry
@Component
struct Index {
  @State username: string = ""
  @State password: string = ""

  build() {
    Flex({ justifyContent: FlexAlign.Center,
      alignItems: ItemAlign.Center,
      direction: FlexDirection.Column }) {
      Text("登录")
        .fontSize(40)
        .fontWeight(700)
      /* 展示输入框输入的数据 */
      Text(this.username)
        .fontSize(40)
        .fontWeight(700)
      Text(this.password)
        .fontSize(40)
        .fontWeight(700)
      MyInput({ placeholder: "输入用户名", inputValue: $username })
      MyInput({ placeholder: "输入密码", inputValue: $password })
      Button("登录")
    }
    .width("100%")
    .height("100%")
    .border({ width: 10, color: "#000" })
  }
}

myInput.ets:

@Component
  /* struct用于定义组件的名称 */
struct MyInput {
  @Link inputValue: string
  @Prop placeholder: string

  build() {
    TextInput({ placeholder: this.placeholder, text: this.inputValue })
      .onChange((value:string) => {
        this.inputValue = value
      })
      .width("80%")
      .height(40)
  }
}

export default MyInput

如下图,实现了父子组件数据的双向绑定:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世界尽头与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值