鸿蒙Next 支持数据双向绑定的组件:Checkbox--Search--TextInput

Checkbox

语法,绑定的变量发生变化时,会触发UI的刷新

javascript 复制代码
@Entry
@Component
struct MvvmCase {
@State
isMarry:boolean = false

  @State
  searchText:string = ''
  build() {
    Grid(){
      GridItem(){
        Column(){
        Text("checkbox 的双向绑定")
          Checkbox()
            .select($$this.isMarry)
          Text(this.isMarry+"")
          Button('改变')
            .onClick(()=>{
              this.isMarry=!this.isMarry
            })
        }
      }
      .height(200)
      .backgroundColor(Color.Yellow)

    }
    .width('100%')
    .height('100%')
    .columnsTemplate("1fr 1fr")
    .columnsGap(20)
    .rowsGap(20)
  }
}

javascript 复制代码
@Entry
@Component
struct MvvmCase {
@State
isMarry:boolean = false

  @State
  searchText:string = ''
  build() {
    Grid(){

      GridItem(){
        Column(){
          Text("search 的双向绑定")
         Search({value:$$this.searchText})
          Text(this.searchText+"")
          Button('改变')
            .onClick(()=>{
              this.searchText='老宋你好'
            })
        }
      }
      .height(200)
      .backgroundColor(Color.Pink)
    }
    .width('100%')
    .height('100%')
    .columnsTemplate("1fr 1fr")
    .columnsGap(20)
    .rowsGap(20)
  }
}

​​​​​​​

TextInput

复制代码
@Entry
@Component
struct MvvmCase {
@State
isMarry:boolean = false

  @State
  searchText:string = ''
  controller: TextInputController = new TextInputController()

  build() {
    Grid(){
      GridItem(){
        Column(){
          Text("TextInput 的双向绑定")
          TextInput({ text: $$this.searchText, placeholder: 'TextInput 的双向绑定', controller: this.controller })
          Text(this.searchText+"")
          Button('改变')
            .onClick(()=>{
              this.searchText='老宋你好01'
            })
        }
      }
      .height(200)
      .backgroundColor(Color.Pink)
    }
    .width('100%')
    .height('100%')
    .columnsTemplate("1fr 1fr")
    .columnsGap(20)
    .rowsGap(20)
  }
}

还有很多组件都支持双向绑定

相关推荐
Madison-No726 分钟前
搭建项目测试环境
linux·运维·服务器·python
+VX:Fegn089540 分钟前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
泡海椒1 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
吴声子夜歌1 小时前
Shell编程实例——编写安全的shell脚本(二)
linux·运维·shell
景熙55232 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
哈__2 小时前
KES-Operator重塑Kubernetes环境下的KES数据库集群管理
数据库·kubernetes·operator·kes
wno7043 小时前
Spring Security权限控制
java·python·spring
xiaoye-duck3 小时前
《Linux 网络编程》深入理解 IP 协议(二):网段划分、私有 IP 与 NAT 地址转换
linux·网络·ip
杨运交3 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring