HarmonyOS @Prop传递对象类型参数时子组件收到的对象数据不同步(API12+)
在如下代码中,首先在Parent父组件中更新controller的属性status,并将controller传入到Child子组件,子组件间中controller通过@Prop装饰器装饰。运行后发现子组件中拿到的controller.status值不对,仍然是初始值0。
@Entry
@Component
struct Parent {
controller: Controller = new Controller()
build() {
Column({ space: 8 }) {
Button('update status in Parent')
.onClick(() => {
this.controller.updateStatus(1);
console.debug(`-abc- status is: ${this.controller.status}`);
})
Child({
controller: this.controller
})
}
}
}
export class Controller {
status: number = 0;
updateStatus(newStatus: number) {
this.status = newStatus;
}
isDone(): boolean {
return this.status === 0 ? false : true
}
}
@Component
struct Child {
@Prop controller: Controller;
build() {
Column() {
Button('Check status in child')
.onClick(() => {
const result = this.controller.isDone();
console.debug(`-abc-Is done? ${result}`);
})
}
}
}
1
浏览38 发布于2026-06-04 09:55未知归属地
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
了解社区公约,与您携手共创和谐专业的开发者社区。





































苏公安网备 32011402010933号