HarmonyOS Webkit ets之间的调用

​有一个封装的webkit。

WebComponentPage.ets
  • 1.

在里面定义一个​。

loadUrl(content:string): void { 
  this.url = content; 
  this.controller.refresh(); 
}
  • 1.
  • 2.
  • 3.
  • 4.

​webkit 交互使用 this.jsBridge。

其中this.jsBridge 方式引入的​。

import { jsBridge } from '../webkit/jsBridge'; 
jsBridge: jsBridge = new jsBridge(); 
 
 
Web({ src: $rawfile(this.url), controller: this.controller }) 
  .javaScriptAccess(true) 
  .fileAccess(true) 
  .imageAccess(true)//设置是否允许自动加载图片资源。 
  .javaScriptProxy({ 
    object: this.jsBridge, 
    name: "jsBridge_jzt", 
    methodList: ["createNewView","loginForPhoneAndCode","login","test"], 
    controller: this.controller 
  })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

​在jsBridge.ets中。

想调用WebComponentPag.ets中的 loadUrl 传递网站参数 实现页面打开新的一页。

我的方案是 在jsBridge.ets中​。

import { WebComponentPage } from '../pages/WebComponentPage' 
mWebComponentPage mWebComponentPage = new mWebComponentPage()
  • 1.
  • 2.

​然后在调用的地方。

this.mWebComponentPage.loadUrl(xxxxxxxx)
  • 1.

这么调用 报错了 麻烦给出个更好的方案​。

HarmonyOS
2024-11-07 10:26:14
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

​可以将mWebComponentPage定义成一个class ,然后调用其中的方法刷新页面。

例如​:

export class MWebComponentPage { 
  private static mWebComponentPage: MWebComponentPage; 
  static instance() { 
    if (!MWebComponentPage.mWebComponentPage) { 
      MWebComponentPage.mWebComponentPage = new MWebComponentPage(); 
    } 
    return MWebComponentPage.mWebComponentPage; 
  } 
  loadUrl() { 
  } 
} 
// 在某个 xx.ets中使用时声明 
MWebComponentPage : mWebComponentPage = MWebComponentPage.instance();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

​或者在Web组件的回调方法中刷新页面,例如onConfirm,onPrompt,onPageEnd等等回调方法,可参考​https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-web-V5#onpageend

分享
微博
QQ
微信
回复
2024-11-07 16:03:51
相关问题
HarmonyOS 不同har包之间怎么调用
695浏览 • 1回复 待解决
ets文件怎么调用ts文件中定义方法
4083浏览 • 1回复 待解决
请问ets项目能调用jschart组件吗?
3496浏览 • 1回复 待解决
HarmonyOS C++ 调用 ets 层函数报错闪退
971浏览 • 1回复 待解决
是否会长期支持ets调用ts代码
2284浏览 • 1回复 待解决
HarmonyOS ets和ts区别
810浏览 • 1回复 待解决
HarmonyOS 关于Ability之间跳转
730浏览 • 1回复 待解决
ETSets没有input,如何实现输入?
9504浏览 • 2回复 待解决
HarmonyOS page之间相互传值
464浏览 • 1回复 待解决
HarmonyOS HAR包之间路由跳转
1376浏览 • 1回复 待解决