HarmonyOS 怎么给H5传递JSONObject对象

controller: web_webview.WebviewController = null!;
this.controller.runJavaScript(`callMessageByFlowNo(${flowNo},${result})`);

通过这种方法,怎么给H5传递JSONObject对象,而不是string类型

jsResult.handlePromptConfirm(JSON.stringify(result))应该怎么传递json对象

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

参考demo:

import { webview } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

class Result {
  a:number
  b:string

  constructor(a: number, b: string) {
    this.a = a;
    this.b = b;
  }
}

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  @State webResult: string = '';
  result:Result = new Result(1,'1')
  build() {
    Column() {
      Text(this.webResult).fontSize(20)
      Web({ src: $rawfile('index4.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          try {
            this.controller.runJavaScript(`test(${JSON.stringify(this.result)})`,
              (error, result) => {
                if (error) {
                  console.error(`run JavaScript error, ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
                  return;
                }
                if (result) {
                  this.webResult = result;
                  console.info(`The test() return value is: ${result}`);
                }
              });
            if (e) {
              console.info('url: ', e.url);
            }
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
    }
  }
}


h5 :
<!-- index.html -->
  <!DOCTYPE html>
  <html>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <script src="https://lib.baomitu.com/vConsole/3.15.0/vconsole.min.js"></script>
  <script>
  window.vConsole = new window.VConsole()
  </script>
  <body>
  Hello world!
  </body>
  <script type="text/javascript">
  function test(result) {

    console.log(result)
    console.log('Ark WebComponent')
    return 'result'
  }
  </script>
  </html>
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 原生怎么主动触发消息h5
86浏览 • 1回复 待解决
HarmonyOS 如何向H5传递cookie值
23浏览 • 1回复 待解决
HarmonyOS h5HarmonyOS怎么通信?
186浏览 • 1回复 待解决
HarmonyOS webview 怎么H5交互
76浏览 • 1回复 待解决
ArkTs怎么传递对象或者类Native
2375浏览 • 1回复 待解决
HarmonyOS web组件加载h5h5拉起摄像头
613浏览 • 1回复 待解决
HarmonyOS 本地H5加载
58浏览 • 1回复 待解决
HarmonyOS H5和原生交互
31浏览 • 1回复 待解决
HarmonyOS webview h5的localstorage
439浏览 • 1回复 待解决
HarmonyOS web与H5交互
739浏览 • 1回复 待解决
HarmonyOS H5如何访问相册?
305浏览 • 1回复 待解决
HarmonyOS h5和ArkTS通信
318浏览 • 1回复 待解决
跨平台开发,h5代码怎么调试?
263浏览 • 1回复 待解决
FA里内嵌H5怎么保持登录状态?
7096浏览 • 1回复 待解决
HarmonyOS webview加载H5显示过慢
28浏览 • 1回复 待解决
HarmonyOS webview h5跳转到应用
21浏览 • 1回复 待解决
HarmonyOS h5 window.history 无效
115浏览 • 1回复 待解决