2

How uniapp webview communicate with each other with H5

2222.gif

h5 package can be obtained from here
git:https://github.com/MyButifullyLife/hybirdUni

enter the theme

1. How uniapp receives messages sent by H5

uniapp side:
Use @message to listen for H5 messages
image.png

H5 end:
index.htmel import uni.webview.js latest version address: https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js
There is a pit here, the JS cannot be loaded when the Android side is running, so you need to add this js code, take it down and import
image.png

The runtime monitors the completion of uniappjs loading. After the loading is complete, you can use window.uni.postMessage to send messages to the uniapp base. send content to be written in data

document.addEventListener('UniAppJSBridgeReady', function(e) {
    uni.getEnv(function(res) {
      console.log('当前环境:' + JSON.stringify(res));
    });

    // 向uniapp底座发送消息
    uni.postMessage({
      data: 'H5发送的消息'
    });
  });

2. uniapp sends a message to H5

image.png


嘉豪
29 声望0 粉丝