鸿蒙桌面小部件postCardAction无法调用EntryAbility中的方法在uni-app中
鸿蒙桌面小部件postCardAction无法调用EntryAbility中的方法在uni-app中
| 类别 | 信息 |
|---|---|
| 产品分类 | uniapp/App |
| PC开发环境 | Windows |
| PC系统版本 | win11 |
| HBuilderX类型 | 正式 |
| HBuilderX版本 | 4.82 |
| 手机系统 | HarmonyOS NEXT |
| 手机系统版本 | HarmonyOS 5.1.1 |
| 手机厂商 | 华为 |
| 手机机型 | p70 pro |
| 页面类型 | vue |
| vue版本 | vue3 |
| 打包方式 | 云端 |
| 项目创建方式 | HBuilderX |
示例代码:
Image($r('app.media.refresh'))
.width(16)
.height(16)
.margin(8)
.onClick(() => {
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params: {
formId: this.formId,
method: 'updateStorageInfoCall',
}
})
})
操作步骤:
- 用uniapp引入鸿蒙原生桌面小部件
预期结果:
- 小部件可以正常刷新
实际结果:
- 小部件刷新的时候报错了
bug描述:
Image($r('app.media.refresh'))
.width(16)
.height(16)
.margin(8)
.onClick(() => {
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params: {
formId: this.formId,
method: 'updateStorageInfoCall',
}
})
})比这个刷新小部件的按钮 就会提示这个错误
04:08:03.449 Callee onRemoteMessageRequest code [number 1] 04:08:03.453 Use new start up rule, check caller permission. 04:08:03.459 Callee onRemoteMessageRequest code proc 04:08:03.459 Callee onRemoteMessageRequest method [updateBatteryInfoCall] 04:08:03.475 Callee onRemoteMessageRequest error, get func is undefined
更多关于鸿蒙桌面小部件postCardAction无法调用EntryAbility中的方法在uni-app中的实战教程也可以访问 https://www.itying.com/category-93-b0.html
求助解决!
更多关于鸿蒙桌面小部件postCardAction无法调用EntryAbility中的方法在uni-app中的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
这个方法并不特殊,是不是 module.json 里没有配置,请提供复现工程和操作步骤,提供更多信息,有助于定位和解答你的问题。
那个鸿蒙官方的小部件示例包私发您了 ,直接在uniapp中的harmony-configs导入entry文件 然后运行到真机 添加小部件 然后点击右上角的刷新就报错了。
回复 2***@qq.com: 你是把整个 entry 都导进去了吗?
回复 DCloud_UNI_OttoJi: 是的
。。。
定位到了 ,就是因为uniapp鸿蒙编译的时候会重置EntryAbility.ets 所以自定义的方法不能写到这个里面。可以使用其他替代方案。
ok,不建议放整个 entry,默认的入口是 uniapp 继承并重写的,建议需要什么就修改什么。
回复 DCloud_UNI_OttoJi: 你好,现在我在EntryAbility.ets 中自定义方法 编译都会被覆盖,像鸿蒙桌面小部件的call和route事件都需要在EntryAbility.ets 写方法,这个EntryAbility.ets不能自己来定义吗?
回复 2***@qq.com: 参考这个 https://uniapp.dcloud.net.cn/tutorial/harmony/runbuild.html#config-dir ,编译产物重新放回到 configs 里。或者你可以编写 vite 插件每次 build 之后操作 fs 修改指定文件。经过我测试编写 ets 组件使用 postCardAction 是正常的
回复 DCloud_UNI_OttoJi: ok
回复 DCloud_UNI_OttoJi: 编译产物(EntryAbility.ets )修改之后再放回harmony-configs, 依然会被覆盖。
回复 2***@qq.com: 可能是你放错了,或者目前编译不支持你这种写法。你可以描述明确你的做法
在鸿蒙系统中,postCardAction 调用 EntryAbility 方法失败通常是由于权限配置或方法声明问题导致的。
检查以下配置:
- ability 声明配置
在
module.json5中确保 EntryAbility 已正确声明:
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"formsEnabled": true
}
]
- 权限配置
在
module.json5中添加必要权限:
"requestPermissions": [
{
"name": "ohos.permission.REQUIRE_FORM"
}
]
- EntryAbility 方法实现 在 EntryAbility.ets 中需要正确定义被调用的方法:
onCreate(want: Want) {
this.callee.on("call", (data: rpc.MessageSequence) => {
const method = data.readString();
if (method === "updateStorageInfoCall") {
// 执行具体逻辑
return;
}
});
}
- 参数传递修正 当前参数传递方式可能需要调整:
postCardAction(this, {
action: 'call',
abilityName: 'EntryAbility',
params: {
method: 'updateStorageInfoCall',
formId: this.formId
}
})


