HarmonyOS 鸿蒙Next webview 注册两个javaScriptProxy 第一个注册的会失效
HarmonyOS 鸿蒙Next webview 注册两个javaScriptProxy 第一个注册的会失效
如何注册两个javaScriptProxy 同时注册两个 后注册的会报找不到
代码
private jsBridge: UnionJsBridge = new UnionJsBridge(this.webController);
private zgphJsBridge: CashierJsBridge = new CashierJsBridge(this.webController,this.toCashierPage);
get javaScriptProxy(): JavaScriptItem {
let result: JavaScriptItem = {
object: {
postMessage: this.postMessage
},
name: “ohosCallNative”,
methodList: [‘postMessage’],
controller: this.controller
}
return result;
}
get javaScriptProxy(): JavaScriptItem {
let result: JavaScriptItem = {
object: {
startPayment: this.startPayment
},
name: “androidZgph”,
methodList: [‘startPayment’],
controller: this.controller
}
return result;
}
Web({ src: this.webUrl, controller: this.webController })
.mixedMode(MixedMode.All)
.javaScriptAccess(true)
.javaScriptProxy(this.jsBridge.javaScriptProxy)
.javaScriptProxy(this.zgphJsBridge.javaScriptProxy)
.cacheMode(CacheMode.Online)
.layoutWeight(1)
.domStorageAccess(true)
.backgroundColor($r(“app.color.color_FFFFFF”))
.onShowFileSelector((event) => {
Log.debug(“onShowFileSelector”)
AppUtil.selectPhoto(1, (data) => {
let uri: string = data.photoUris[0];
event?.result.handleFileList([uri]);
})
return true
})
.onPageBegin(e => {
Log.debug(this.webUrl)
})
log
[CONSOLE:106] “TypeError: Cannot read properties of undefined (reading ‘postMessage’)”
找不到注册对象
代码
private jsBridge: UnionJsBridge = new UnionJsBridge(this.webController);
private zgphJsBridge: CashierJsBridge = new CashierJsBridge(this.webController,this.toCashierPage);
get javaScriptProxy(): JavaScriptItem {
let result: JavaScriptItem = {
object: {
postMessage: this.postMessage
},
name: “ohosCallNative”,
methodList: [‘postMessage’],
controller: this.controller
}
return result;
}
get javaScriptProxy(): JavaScriptItem {
let result: JavaScriptItem = {
object: {
startPayment: this.startPayment
},
name: “androidZgph”,
methodList: [‘startPayment’],
controller: this.controller
}
return result;
}
Web({ src: this.webUrl, controller: this.webController })
.mixedMode(MixedMode.All)
.javaScriptAccess(true)
.javaScriptProxy(this.jsBridge.javaScriptProxy)
.javaScriptProxy(this.zgphJsBridge.javaScriptProxy)
.cacheMode(CacheMode.Online)
.layoutWeight(1)
.domStorageAccess(true)
.backgroundColor($r(“app.color.color_FFFFFF”))
.onShowFileSelector((event) => {
Log.debug(“onShowFileSelector”)
AppUtil.selectPhoto(1, (data) => {
let uri: string = data.photoUris[0];
event?.result.handleFileList([uri]);
})
return true
})
.onPageBegin(e => {
Log.debug(this.webUrl)
})
log
[CONSOLE:106] “TypeError: Cannot read properties of undefined (reading ‘postMessage’)”
找不到注册对象
2 回复
Cannot read properties of undefined
这提示不是空指针吗,你的 this.postMessage this不到东西,换成写死的字符串试试看。