鸿蒙插槽?全局插槽和局部插槽?数据不更新?

@Builder的基本语法数据是不会响应式的

第一种:
全局插槽:传入的变量是@state数据(数据是对象也一样),但是button点击更改,并没有任何反应。规则就是不更新

@Entry
@Component
struct Demo02 {
  @State message: string = 'Hello World'

  build() {
    Row() {
      Column() {
        show(this.message)
        Button('点击').onClick((event: ClickEvent) => {
          this.message = "666"
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
//全局插槽
@Builder
function show(content:string){
    Row(){
      Text(content)
    }
}

第二种:局部插槽:传入的变量是@state数据(数据是对象也一样),但是button点击更改,并没有任何反应。规则就是不更新

@Entry
@Component
struct Demo03 {
  @State message: string = 'Hello World'
  @Builder
  show(content:string){
    Row(){
      Text(content)
    }
  }
  build() {
    Row() {
      Column() {
          this.show(this.message)
        Button("点击").onClick((event: ClickEvent) => {
          this.message = "666"
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

@Builder的新语法数据可以响应式

写法需要注意:
1、传入的需要时对象,$$是官方提供的语法糖,只是一个代号
2、 this.show({name:this.msg.name}) 这个写法是最重要的,传入的必须写成展开对象,并且属性值是变量。直接传this.msg做不到响应式(这是个大坑

@Entry
@Component
struct Demo03 {
  @State message: string = 'Hello World'
  @State msg:A = {
    name:'zhangsan'
  }
  @Builder
  show($$:A){
    Row(){
      Text($$.name)
    }
  }
  build() {
    Row() {
      Column() {
          this.show({name:this.msg.name})
           Button("点击").onClick((event: ClickEvent) => {
          this.msg.name = "lisi"
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

class A {
  name:string = ''
}

鸿蒙-传智播客-博学谷

相关推荐

  1. 鸿蒙全局局部数据更新

    2023-12-27 10:32:03       56 阅读
  2. slot

    2023-12-27 10:32:03       54 阅读
  3. Vue 3

    2023-12-27 10:32:03       64 阅读
  4. Vue 讲解

    2023-12-27 10:32:03       51 阅读
  5. vue<span style='color:red;'>插</span><span style='color:red;'>槽</span>

    vue

    2023-12-27 10:32:03      43 阅读
  6. react

    2023-12-27 10:32:03       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 10:32:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 10:32:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 10:32:03       82 阅读
  4. Python语言-面向对象

    2023-12-27 10:32:03       91 阅读

热门阅读

  1. Crow:Middlewares 庖丁解牛7 after_handlers_call_helper

    2023-12-27 10:32:03       57 阅读
  2. [c]扫雷

    [c]扫雷

    2023-12-27 10:32:03      68 阅读
  3. 轻量级Python IDE使用(四)——类

    2023-12-27 10:32:03       50 阅读
  4. Node.js(三)-模块的加载机制

    2023-12-27 10:32:03       45 阅读
  5. 如何避免卡尔曼滤波器的发散? Q P R参数怎么调?

    2023-12-27 10:32:03       56 阅读
  6. 修改docker镜像IP

    2023-12-27 10:32:03       59 阅读
  7. asp.net core webapi AutoMapper使用

    2023-12-27 10:32:03       54 阅读