鸿蒙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)
  }
}

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

相关推荐
朦胧之4 小时前
AI 编程-老项目改造篇
java·前端·后端
程序猿大帅9 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
AlfredZhao9 小时前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
程序员晓琪10 小时前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly10 小时前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
众少成多积小致巨10 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
东坡白菜11 小时前
破局全栈:前端开发的Java入门实战记录—JPA(2)
java·后端
SimonKing17 小时前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户2986985301417 小时前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端
倔强的石头_19 小时前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库