HarmonyOS 鸿蒙Next H5和Arkts交互,H5页面首次获取不到原生侧接口返回数据
HarmonyOS 鸿蒙Next H5和Arkts交互,H5页面首次获取不到原生侧接口返回数据
getCache(parameter: Object): Promise<string> {
Logger.error(‘getCache调用’, parameter.toString());
this.KeyValueBean = JSON.parse(parameter.toString())
return new Promise((resolve) => {
StorageUtils.get(this.KeyValueBean.key,this.KeyValueBean.content).then((result) => {
Logger.error(‘getCache返回2’, result.toString());
resolve(result.toString());
})
})
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
方案2 : 断点打印先实行的 return this.mFruit;返回为空 然后才打印log
这种第一次点击接收不到值,第二次才能接受的
getCache(parameter: Object): string {
Logger.error(‘getCache调用’, parameter.toString());
this.KeyValueBean = JSON.parse(parameter.toString())
StorageUtils.get(this.KeyValueBean.key,this.KeyValueBean.content).then((result) => {
this.mFruit = result.toString();
Logger.error(‘getCache返回2’, this.mFruit);
})
return this.mFruit;
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next H5和Arkts交互,H5页面首次获取不到原生侧接口返回数据的实战教程也可以访问 https://www.itying.com/category-93-b0.html
方案1: 将 ets 和 html中的方法都改成异步调用
// xxx.ets 中异步方法
async getCache(parameter: Object): Promise<string> {
console.log(“打印=”+parameter.toString())
this.KeyValueBean = JSON.parse(parameter.toString())
let result = await StorageUtils.get(this.KeyValueBean.key,this.KeyValueBean.content);
return result.toString();
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
// xxx.html 调用处也改成异步调用
window.jsBridge_jzt.getCache(JSON.stringify(json)).then((res)=>{
$("#result").text(res);
})
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
方案2: 将 ets 和 html中的方法都改成同步调用,同时将所以调用首选项的异步方法都改成同步的(带Sync的方法)
// xxx.ets写法
getCache(parameter: Object): string {
console.log(“打印=”+parameter.toString())
this.KeyValueBean = JSON.parse(parameter.toString())
// 方法里调用数据库方法改成同步方法
let result = StorageUtils.getSync(this.KeyValueBean.key,this.KeyValueBean.content) ;
this.mFruit = result;
console.log(“打印返回数据”+parameter.toString())
return this.mFruit;
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
// xxx.html 写法
$("#result").text(window.jsBridge_jzt.getCache(JSON.stringify(json)));
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next H5和Arkts交互,H5页面首次获取不到原生侧接口返回数据的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
针对HarmonyOS鸿蒙Next中H5和ArkTS交互时,H5页面首次获取不到原生侧接口返回数据的问题,以下是一些可能的解决方案:
- 检查加载顺序:确保H5页面在尝试获取数据之前,原生侧的数据已经准备好且接口已被正确调用。
- 异步处理:原生侧接口可能是异步的,H5页面需要实现适当的回调或Promise机制来处理异步数据。
- 权限配置:验证H5页面是否有足够的权限访问原生侧提供的数据或服务。
- 接口配置:检查接口配置是否正确,包括接口路径、参数等。
- 通信桥梁:确保用于H5与原生通信的桥梁(如JSBridge)配置正确,且两端都按照约定的方式使用。
- 调试与日志:在原生侧和H5页面加入适当的日志记录,使用调试工具追踪数据流动和错误发生的位置。
如果以上方法均未能解决问题,建议检查H5页面和原生侧的实现代码,确认没有逻辑错误或遗漏。同时,也可以尝试在多个不同的环境中测试H5页面与原生侧的交互,以确定问题是否与特定环境有关。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。