@dead_fish/jsbridge
v1.0.23
Published
A lightweight and easy-to-use jsBridge for Android and IOS and HarmonyOS platforms.
Maintainers
Readme
npm install @dead_fish/jsbridge适用于 IOS 和 Android 以及 HarmonyOS 平台
Usage
API
registerHandler
注册一个供 Native 调用的处理器。
| 参数 | 类型 | 说明 |
| --------- | -------- | ------------------------------------------------ |
| eventName | string | 事件名称 |
| handler | function | 处理函数,接收 data 和 responseCallback 参数 |
callHandler
调用 Native 端的处理器。
| 参数 | 类型 | 默认值 | 说明 | | --------- | -------- | --------------------------- | ------------------------------------ | | eventName | string | - | 事件名称 | | handler | function | - | 回调函数,接收 Native 返回的数据 | | data | any | "" | 传递给 Native 的数据 | | injectObj | string | "ohosCallNative.callNative" | 鸿蒙系统下调用 Native 的全局对象路径 |
ES
import { jsBridge, utils } from "@dead_fish/jsbridge";
const query = utils.parseUrl();
jsBridge.registerHandler({
eventName: "eventName",
handler: function (data, callback) {
console.log(data);
},
});
jsBridge.callHandler({
eventName: "eventName",
handler: function (response) {
console.log(response);
},
data: {},
injectObj: "ohosCallNative.callNative",
});CommonJS
const { jsBridge, utils } = require("@dead_fish/jsbridge");
const query = utils.parseUrl();
jsBridge.registerHandler({
eventName: "eventName",
handler: function (data, callback) {
console.log(data);
},
});
jsBridge.callHandler({
eventName: "eventName",
handler: function (response) {
console.log(response);
},
data: {},
injectObj: "ohosCallNative.callNative",
});Browser
<script src="./node_modules/@dead_fish/jsbridge/dist/bundle.umd.js"></script>
<script>
const jsBridge = window.dhLib.jsBridge;
const utils = window.dhLib.utils;
const query = utils.parseUrl();
jsBridge.registerHandler({
eventName: "eventName",
handler: function (data, callback) {
console.log(data);
},
});
jsBridge.callHandler({
eventName: "eventName",
data: {},
handler: function (response) {
console.log(response);
},
});
</script>