vue-webview-js-bridge
- WebviewJavascriptBridge plugin for Vue.js
- 基于WebViewJavascriptBridge(ios), JsBridge(android)开发
- Promise封装,支持
then
或者async/await
等方式
安装
yarn:
yarn add vue-webview-js-bridge
npm:
npm i vue-webview-js-bridge
Example
// main.js Vue // component.vue name: 'HelloWorld' props: msg: String { return code: '' } { this$bridge } methods: async { try let res = await this$bridge thiscode = res catch error console }
TypeScript 支持
main.ts
// ... Vue.useVueJsBridge, option// ...
component.vue
配置参数(Options)
debug
- type: boolean
- default:
true
- description: 输出调用信息
delay
- type: number
- default:
200
- description: 由于birdge初始化需要时间导致的registerHandler失败的处理,延时调用时间,单位
ms
native调用前端注册的方法最好也要延时处理,避免前端还未注册时候native调用导致的问题
nativeHandlerName
- type: string, 必填项
- default:
'nativeHandler'
- description: 和原生开发人员协商的nativeHandlerName
mock
- type: boolean
- default:
true
- description: 开发阶段是否开启mock服务,需要配合mockHandler使用,两者都设置的情况下mock生效
mockHandler
- type: Function
- default:
null
- description: 开发阶段mockHandler服务,需要配合mock使用,两者都设置的情况下mock生效. 是一个函数,第一个参数接收payload, 第二个参数接受bridge回调函数
{ // mock by payload // switch(payload) { // case 1: // next(mockData) // break // ... // } // call next(data) to mock data}
提供的方法(Methods)
registerHandler
- description:注册一个bridge提供给原生开发者调用,第一个参数name(和原生开发者协商好的bridgeName),第二个参数callback函数,
- callback: callback函数接收两个参数,第一个参数是native传来的数据data,第二个参数是原生传来的responseCallback,当需要时通知native我们的状态
this$bridge
callHandler
- description: 接受一个参数payload(和原生开发人员协商格式)例如:
this$bridge
Todo
- 增加单元测试
- 增加 TypeScript types 支持