鸿蒙this指向导致BuilderParam数据显示异常问题解决
鸿蒙this指向导致BuilderParam数据显示异常问题解决
up对鸿蒙的学习已经渐渐熟悉。所以,从即日起,开始更新鸿蒙相关的文章,用于记录和分享学习鸿蒙遇到的问题和问题的解决方法。
环境:devceo 5.0.2
api版本: 12
对应openharmonry版本: 5.0.0
一、问题产生的原因
现在假设一个场景
有一个父组件:page1
import { comp1 } from './test'
import { promptAction } from '@kit.ArkUI'
import appLog from '../../log/log'
@Entry
@Component
struct page1 {
@State msg: string = "我是父组件的状态msg"
@Builder
builder1() {
Column() {
Text(this.msg)
.backgroundColor('#ffec8080')
.width('100vp')
.height('100vp')
.onClick(()=>{
appLog.log("builder1被点击了")
this.msg="hhhhhhh"
promptAction.showToast({message:"11111"})
})
}
}
build() {
Column(){
Text("白色内为根组件")
comp1({
txt: this.builder1
})
}
.width('100%')
.height('100%')
}
}
一个子组件: comp1
import appLog from "../../log/log";
@Component
export struct comp1 {
@Builder
default(){}
@BuilderParam txt:()=>void=this.default;
build() {
Column(){
Text("绿色内部为子组件")
this.txt()
}
.onClick(()=>{
appLog.log("子组件被点击了")
})
.backgroundColor('#ffa9ffa9')
.width('50%')
.height('50%')
}
}
这个子组件里面需要传入一个自定义的组件
@BuilderParam txt:()=>void
但是我们想要传入子组件 的自定义组件包含了父组件 的状态变量
@State msg: string = "我是父组件的状态msg"
@Builder
builder1() {
Column() {
Text(this.msg) //使用了父组件的状态变量
.backgroundColor('#ffec8080')
.width('100vp')
.height('100vp')
.onClick(()=>{
appLog.log("builder1被点击了")
this.msg="hhhhhhh"
promptAction.showToast({message:"11111"})
})
}
}
这个builder1构建函数中我们使用了父组件的状态变量msg
这个时候直接将builder1函数传入子组件会导致this指向问题(子组件找不到this.msg),从而导致Text中不显示任何文字
ps:如果没有设置宽高还会导致这个组件不会渲染
例如:

二、解决办法
解决方法一、
通过查阅官方文档、有这样的说明
需要注意this的指向
以下示例对this的指向做了介绍。
@Component
struct Child {
label: string = `Child`;
@Builder customBuilder() {};
@Builder customChangeThisBuilder() {};
@BuilderParam customBuilderParam: () => void = this.customBuilder;
@BuilderParam customChangeThisBuilderParam: () => void = this.customChangeThisBuilder;
build() {
Column() {
this.customBuilderParam()
this.customChangeThisBuilderParam()
}
}
}
@Entry
@Component
struct Parent {
label: string = `Parent`;
@Builder componentBuilder() {
Text(`${this.label}`)
}
build() {
Column() {
// 调用this.componentBuilder()时,this指向当前@Entry所装饰的Parent组件,即label变量的值为"Parent"。
this.componentBuilder()
Child({
// 把this.componentBuilder传给子组件Child的@BuilderParam customBuilderParam,this指向的是子组件Child,即label变量的值为"Child"。
customBuilderParam: this.componentBuilder,
// 把():void=>{this.componentBuilder()}传给子组件Child的@BuilderParam customChangeThisBuilderParam,
// 因为箭头函数的this指向的是宿主对象,所以label变量的值为"Parent"。
customChangeThisBuilderParam: (): void => { this.componentBuilder() }
})
}
}
}
官方文档通过
将customBuilderParam: this.componentBuilder转变为
customChangeThisBuilderParam: (): void => { this.componentBuilder() }
的方式 会将this指向定向至父组件
通过这样修改,大概率就可以正常显示文字
comp1({
txt:():void=>{ this.builder1()}
})
解决方法二、
还有一个阴招,在子组件中声明和父组件一样的变量
例如:
父组件调用方式不变
comp1({
txt:this.builder1
})
子组件内
import appLog from "../../log/log";
@Component
export struct comp1 {
@State msg:string="默认信息"
@Builder
default(){}
@BuilderParam txt:()=>void=this.default;
build() {
Column(){
Text("绿色内部为子组件")
this.txt()
}
.onClick(()=>{
appLog.log("子组件被点击了")
})
.backgroundColor('#ffa9ffa9')
.width('50%')
.height('50%')
}
}
这样也可以正常显示,当你修改父组件的msg时,子组件也会改变,不过这样会导致this指向混乱,不到万不得已还是不要用吧。
更多推荐
所有评论(0)