Q:应用侧通过registerJavaScriptProxy注入方法到H5中,并在H5调用该方法并传入对象参数,请问应用侧如何获取H5返回对象的全部键值?
A:通过Object.keys()方法获取对象键属性名组成的数组。
import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';
class jsBridgeClass {
constructor() {
}
// 通过Object.keys()方法获取对象键属性名组成的数组
// 通过Reflect.get()方法获取对应对象属性的value值
getObjectKeyValue(obj: ESObject) {
for (const key of Object.keys(obj)) {
console.info('获取类对象的属性和值', key, Reflect.get(obj, key));
// 也可以通过obj[key]获取对象属性的value值
console.info('获取类对象的属性和值', key, obj[key]);
}
}
}
@Entry
@Component
struct Index {
@State jsBridgeObj: jsBridgeClass = new jsBridgeClass();
private webviewController: WebviewController = new webview.WebviewController();
build() {
Column() {
Web({
src: $rawfile('getObjectKeyValue.html'),
controller: this.webviewController,
})
.width('100%')
.height('100%')
.javaScriptAccess(true)
.domStorageAccess(true)
.fileAccess(false)
.geolocationAccess(false)
.onControllerAttached(() => {
try {
this.webviewController.registerJavaScriptProxy(this.jsBridgeObj, 'jsBridgeObjName', ['getObjectKeyValue']);
} catch (error) {
console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
}
});
};
}
} -->
<!DOCTYPE html>
<html>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<body>
<button onclick="callArkTS()" type="button">Click Me!</button>
</body>
<script>
function callArkTS() {
let obj ={
id:1,
name:'LILY'
}
jsBridgeObjName.getObjectKeyValue(obj);
}
</script>
</html>
<!--