751496032 / dsbridge-harmonyos Goto Github PK
View Code? Open in Web Editor NEW鸿蒙原生ArkTS与JavaScript的通信框架
鸿蒙原生ArkTS与JavaScript的通信框架
错误如下:
hvigor ERROR: Failed :entry:default@MergeProfile...
hvigor ERROR: The compatibleSdkVersion 9 cannot be smaller than version 11 declared in library [:library]
as the library might be using APIS not available in 9
看起来像是只支持11以上的项目了
暂不支持API命名空间,后续会根据需求决定是否支持?
这个什么时候能支持,我们有这个需求,跟安卓一样的模式
使用demo, js 可以调用 原生的 testSync 方法, 但是 原生调用 showAlert 没反应,callJs 和 callHandler 都不行
现在报错后会弹出toast,应该让业务自行捕获错误处理(toast or 忽略)。
提示Script error,根据安卓DSBridge工程readme判断。不确定是否正确。js调用原生正常。
按照文档,方法必须写到单独的类中,应为牵扯到很多业务及,多个页面可能有相同交互方法,但是处理逻辑不一致;如果不想分多个JsBridge类,我怎样可以直接在鸿蒙的Component中直接定义方法供JS调用
试了下原生的harmonyOS的 web组件是可以触发的
let jsParam: Parameter = this.safeParse(params)
// todo 待优化
async = (jsParam._dscbstub != null && jsParam._dscbstub !== undefined) || async
LogUtils.d("async: " + async + " --- " + methodName)
let data: string = (this.isObject(**jsParam.data**) ? JSON.stringify(**jsParam.data**) : **jsParam.data**) as string
原生调用call方法,没有拿到前端返回的数据
这种情况使用DSBridge怎么处理呢?
@hzw/ohos-dsbridge 的版本是多少
this.cHandler.complete(Object) //事变
export type Args = number | string | boolean
缺少Object支持,希望 改成export type Args = number | string | boolean |Object 对返回对象也进行支持
这里也改
export interface CallResult {
code: number,
data?: string | boolean | number| Object,
errMsg?: string,
async?: boolean,
}
目前我自己改了没问题。我的问题解决
感谢作者!
比如 methodName 参数为 undefined, 会导致应用闪退。由于这个是h5页面调用的,无法保证传入参数一定正确,在处理参数前需要做一些必要的检测,减少h5页面对应用的影响。
通过JavaScriptInterface声明的方法应该如何同步所在页面组件状态
如果通过javaScriptProxy或registerJavaScriptProxy来注册方法,示例:
import web_webview from '@ohos.web.webview';
interface ArkTSFunModel {
jumpOrderConfirm: (detailStr: string) => void;
}
@Entry
@Component
struct WebComponent {
webviewController: web_webview.WebviewController = new web_webview.WebviewController();
hybridehChooseOptionDialog: CustomDialogController = new CustomDialogController({
builder: HybridehChooseOptionDialog({
onFirst:()=>{ },
onSecond:()=>{
}
}),
alignment: DialogAlignment.Bottom
})
// 声明需要注册的对象
arkTSObj: ArkTSFunModel = {
jumpOrderConfirm: (detailStr: string) => this.jumpOrderConfirm(detailStr)
};
jumpOrderConfirm(detailStr: string): void {
//
}
build() {
Column() {
// web组件加载本地index.html页面
Web({ src: $rawfile('index.html'), controller: this.webviewController})
// 将对象注入到web端
.javaScriptProxy({
object: this.arkTSObj,
name: 'arkTSFunObj',
methodList: ['jumpOrderConfirm'],
controller: this.controller
})
}
}
}
这样arkTSObj中实现的所有方法可以访问WebComponent 下的所有属性方法。
通过JavaScriptInterface声明的方法有什么好的办法做到同样的事呢? 脱离Component直接声明CustomDialogController并调用方法open试了下好像不行,没反应。
仓库代码,可以直接运行到鸿蒙手机吗,我拉下来,好像不可以,谢谢了
大佬异步方法内有await就直接返回 undefined
很多方法都需要await 这边能够兼容么
1、正常返回
@JavaScriptInterface()
getInfo(args: string, handler: CompleteHandler) {
handler.complete('{"x":1}');
}
2、正常返回
@JavaScriptInterface()
async getInfo(args: string, handler: CompleteHandler) {
handler.complete('{"x":1}');
}
3、返回 undefined
@JavaScriptInterface()
async getInfo(args: string, handler: CompleteHandler) {
await new Promise(resolve => setTimeout(resolve, 100))
handler.complete('{"x":1}');
}
this.controller. getJavaScriptProxy()此方法在api12中提示已废弃
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.