HarmonyOS webview刷新问题

WebvIew 中,runJavaScriptExt() app 通过此方法调js里面的方法,

js 代码

function test1() {
}
function test2(str) {
}

app调test1()是 this.controller.runJavaScriptExt(‘test1()’)

那如何调test2(), 参数如何携带,实现app调用js里面带参数的方法

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

参考以下代码:

import web_webview from '@ohos.web.webview';
import business_error from '@ohos.base';

@Entry
@Component
struct WebComponent {
  controller: web_webview.WebviewController = new web_webview.WebviewController();
  @State msg1: string = ''
  @State msg2: string = ''

  build() {
    Column() {
      Text(this.msg1).fontSize(20)
      Text(this.msg2).fontSize(20)
      Web({ src: $rawfile('index.html'), controller: this.controller })
        .javaScriptAccess(true)
        .onPageEnd(e => {
          try {
            this.controller.runJavaScriptExt(
              "test(\"" + '哈哈' + "\")",
              (error, result) => {
                if (error) {
                  let e: business_error.BusinessError = error as business_error.BusinessError;
                  console.error(`run JavaScript error, ErrorCode: ${e.code},  Message: ${e.message}`)
                  return;
                }
                if (result) {
                  try {
                    let type = result.getType();
                    switch (type) {
                      case web_webview.JsMessageType.STRING: {
                        this.msg1 = "result type:" + typeof (result.getString());
                        this.msg2 = "result getString:" + ((result.getString()));
                        break;
                      }
                      case web_webview.JsMessageType.NUMBER: {
                        this.msg1 = "result type:" + typeof (result.getNumber());
                        this.msg2 = "result getNumber:" + ((result.getNumber()));
                        break;
                      }
                      case web_webview.JsMessageType.BOOLEAN: {
                        this.msg1 = "result type:" + typeof (result.getBoolean());
                        this.msg2 = "result getBoolean:" + ((result.getBoolean()));
                        break;
                      }
                      case web_webview.JsMessageType.ARRAY_BUFFER: {
                        this.msg1 = "result type:" + typeof (result.getArrayBuffer());
                        this.msg2 = "result getArrayBuffer byteLength:" + ((result.getArrayBuffer().byteLength));
                        break;
                      }
                      case web_webview.JsMessageType.ARRAY: {
                        this.msg1 = "result type:" + typeof (result.getArray());
                        this.msg2 = "result getArray:" + result.getArray();
                        break;
                      }
                      default: {
                        this.msg1 = "default break, type:" + type;
                        break;
                      }
                    }
                  }
                  catch (resError) {
                    let e: business_error.BusinessError = resError as business_error.BusinessError;
                    console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
                  }
                }
              });
            if (e) {
              console.info('url: ', e.url);
            }
          } catch (error) {
            let e: business_error.BusinessError = error as business_error.BusinessError;
            console.error(`ErrorCode: ${e.code},  Message: ${e.message}`);
          }
        })
    }
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS 页面刷新问题
340浏览 • 1回复 待解决
HarmonyOS 组件刷新问题
85浏览 • 1回复 待解决
HarmonyOS 列表刷新问题
665浏览 • 1回复 待解决
HarmonyOS 数据刷新问题
103浏览 • 1回复 待解决
HarmonyOS ForEach列表刷新问题
84浏览 • 1回复 待解决
HarmonyOS List item 刷新问题
817浏览 • 1回复 待解决
HarmonyOS 页面数据刷新问题
42浏览 • 1回复 待解决
HarmonyOS @Builder UI刷新问题
36浏览 • 1回复 待解决
HarmonyOS UI 未刷新问题
580浏览 • 0回复 待解决
HarmonyOS UI不刷新问题
35浏览 • 1回复 待解决
HarmonyOS swiper数据刷新问题
46浏览 • 1回复 待解决
HarmonyOS 页面跳转刷新问题
954浏览 • 1回复 待解决
NativeWindowFlushBuffer接口刷新问题
2020浏览 • 1回复 待解决
HarmonyOS使用Refresh下拉刷新问题
945浏览 • 1回复 待解决
HarmonyOS 组件的下拉刷新问题
518浏览 • 1回复 待解决
HarmonyOS 装饰器不刷新问题
239浏览 • 1回复 待解决
HarmonyOS 二维数组刷新问题
342浏览 • 1回复 待解决
HarmonyOS ForEach创建的视图刷新问题
49浏览 • 1回复 待解决
HarmonyOS 自定义弹窗刷新问题
77浏览 • 1回复 待解决
HarmonyOS 状态变量不刷新问题
523浏览 • 1回复 待解决