找回密码
 立即注册
首页 业界区 安全 组件内部尝试模拟页面onPageShow

组件内部尝试模拟页面onPageShow

佴莘莘 2025-5-31 23:46:26
尝试在父组件中改变子组件UI中未用到的状态变量【失败】

一个页面入口文件,一个组件
子组件用@Prop不行,用@Link也不行
1.png

点击查看 Index.ets 代码
  1. import { Text01 } from './text01'
  2. @Entry
  3. @Component
  4. struct Index {
  5.   @State aa: boolean = false
  6.   build() {
  7.     Column() {
  8.       Text01({ aa: this.aa })
  9.       Button('改变状态变量')
  10.         .onClick(() => {
  11.           this.aa = true
  12.         })
  13.     }
  14.     .height('100%')
  15.     .width('100%')
  16.   }
  17. }
复制代码
点击查看 text01.ets 代码
  1. @Component
  2. export struct Text01 {
  3.   @Link aa: boolean
  4.   @State tex: string = '我没有改变'
  5.   aboutToAppear(): void {
  6.     if (this.aa) {
  7.       this.tex = '我被改变了'
  8.     }
  9.   }
  10.   build() {
  11.     Column() {
  12.       Text(this.tex)
  13.     }
  14.   }
  15. }
复制代码
尝试使用onAreaChange事件【失败】

一个组件,两个页面入口文件
Page02.ets文件内容为默认内容,创建之后没有修改
使用router跳转至其他页面,再返回该页面,没有触发onAreaChange事件
2.png

点击查看 Index.ets 代码
  1. import { Text01 } from './text01'
  2. import { router } from '@kit.ArkUI'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   build() {
  7.     Column() {
  8.       Text01()
  9.       Button('跳转至Page02')
  10.         .onClick(() => {
  11.           router.pushUrl({
  12.             url: 'pages/Page02'
  13.           })
  14.         })
  15.     }
  16.     .height('100%')
  17.     .width('100%')
  18.   }
  19. }
复制代码
点击查看 text01.ets 代码
  1. @Component
  2. export struct Text01 {
  3.   @State tex: string = '我没有改变'
  4.   build() {
  5.     Column() {
  6.       Text(this.tex)
  7.         .onAreaChange((oldValue: Area, newValue: Area) => {
  8.           console.log("aaa", JSON.stringify(oldValue))
  9.           console.log("aaa", JSON.stringify(newValue))
  10.         })
  11.     }
  12.   }
  13. }
复制代码
使用onPageShow方法,更新子组件UI【失败】

一个组件,两个页面入口文件
Page02.ets文件内容为默认内容,创建之后没有修改
使用router跳转至其他页面,再返回该页面,触发入口页面组件的onPageShow方法
3.png

点击查看 Index.ets 代码
  1. import { Text01 } from './text01'
  2. import { router } from '@kit.ArkUI'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   @State aa: number = 0
  7.   onPageShow(): void {
  8.     this.aa++
  9.     console.log("aa", this.aa)
  10.     // aa可以改变,没有问题
  11.   }
  12.   build() {
  13.     Column() {
  14.       Text01({ aa: this.aa })
  15.       Button('跳转至Page02')
  16.         .onClick(() => {
  17.           router.pushUrl({
  18.             url: 'pages/Page02'
  19.           })
  20.         })
  21.     }
  22.     .height('100%')
  23.     .width('100%')
  24.   }
  25. }
复制代码
点击查看 text01.ets 代码
  1. @Component
  2. export struct Text01 {
  3.         @Prop aa: number = 0
  4.         @State tex: string = '我没有改变'
  5.         aboutToAppear(): void {
  6.                 if (this.aa == 2) {
  7.                         this.tex = '我该变为2'
  8.                 }
  9.         }
  10.         build() {
  11.                 Column() {
  12.                         Text(`${this.aa}`)
  13.                         Text(this.tex)
  14.                                 .onAreaChange((oldValue: Area, newValue: Area) => {
  15.                                         console.log("aaa", JSON.stringify(oldValue))
  16.                                         console.log("aaa", JSON.stringify(newValue))
  17.                                 })
  18.                 }
  19.         }
  20. }
复制代码


  • 在自定义组件首次渲染时,会记录状态变量与组件的映射关系,当状态变量发生变化时,驱动其相关的组件刷新
  • 框架可以知道该状态变量管理了哪些UI组件,以及这些UI组件对应的更新函数。执行更新函数,实现最小化更新
  • 我以前还以为触发子组件重新渲染,就会重新触发子组件的aboutToAppear方法,这是不对的。
    aboutToAppear方法的触发,只有在创建子组件实例时触发,其余时间不会再次触发。
if...else【成功】

有损性能,能少用就少用,通过合理的业务逻辑规划,最大程度的减少该方法使用
一个组件,两个页面入口文件
Page02.ets文件内容为默认内容,创建之后没有修改
使用router跳转至其他页面,再返回该页面,触发入口页面组件的onPageShow方法与onPageHide方法
点击查看 Index.ets 代码
  1. import { Text01 } from './text01'
  2. import { router } from '@kit.ArkUI'
  3. @Entry
  4. @Component
  5. struct Index {
  6.         @State aa: number = 0
  7.         @State bol: boolean = true
  8.         onPageShow(): void {
  9.                 this.aa++
  10.                 console.log("aa", this.aa)
  11.                 // aa可以改变,没有问题
  12.                 this.bol = true
  13.         }
  14.         onPageHide(): void {
  15.                 this.bol = false
  16.         }
  17.         build() {
  18.                 Column() {
  19.                         if (this.bol) {
  20.                                 Text01({ aa: this.aa })
  21.                         }
  22.                         Button('跳转至Page02')
  23.                                 .onClick(() => {
  24.                                         router.pushUrl({
  25.                                                 url: 'pages/Page02'
  26.                                         })
  27.                                 })
  28.                 }
  29.                 .height('100%')
  30.                 .width('100%')
  31.         }
  32. }
复制代码
点击查看 text01.ets 代码
  1. @Component
  2. export struct Text01 {
  3.         @Prop aa: number = 0
  4.         @State tex: string = '我没有改变'
  5.         aboutToAppear(): void {
  6.                 if (this.aa == 2) {
  7.                         this.tex = '我该变为2'
  8.                 }
  9.         }
  10.         build() {
  11.                 Column() {
  12.                         Text(`${this.aa}`)
  13.                         Text(this.tex)
  14.                                 .onAreaChange((oldValue: Area, newValue: Area) => {
  15.                                         console.log("aaa", JSON.stringify(oldValue))
  16.                                         console.log("aaa", JSON.stringify(newValue))
  17.                                 })
  18.                 }
  19.         }
  20. }
复制代码
onVisibleAreaChange【成功】

官方文档
该方法由这位同学教我,在此感谢该同学

  • 组件可见区域发生变化时触发该回调
    4.png

点击查看 Index.ets 代码
  1. import { Text01 } from './text01'
  2. import { router } from '@kit.ArkUI'
  3. @Entry
  4. @Component
  5. struct Index {
  6.   build() {
  7.     Column() {
  8.       Text01()
  9.       Button('跳转至Page02')
  10.         .onClick(() => {
  11.           router.pushUrl({
  12.             url: 'pages/Page02'
  13.           })
  14.         })
  15.     }
  16.     .height('100%')
  17.     .width('100%')
  18.   }
  19. }
复制代码
点击查看 text01.ets 代码
  1. @Component
  2. export struct Text01 {
  3.   @State tex: string = '子组件'
  4.   build() {
  5.     Column() {
  6.       Text(this.tex)
  7.         .onVisibleAreaChange([0.0, 1.0], (isVisible: boolean, currentRatio: number) => {
  8.           console.log("子组件isVisible:", isVisible)
  9.           console.log("子组件currentRatio:", currentRatio)
  10.         })
  11.     }
  12.   }
  13. }
复制代码


  • 结果:
    5.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册