当前位置: 首页 > news >正文

uniapp webview实现双向通信

需求:uniapp webview嵌套一个h5 实现双向通信

  • uniapp 代码
<template><view><web-view src="http://192.168.3.150:9003/" @message="onMessage"></web-view></view>
</template><script>export default {data(){return{currentWebview:null}},mounted() {const self = this;self.currentWebview = self.$scope.$getAppWebview().children()[0]let info = {name:'hello'}self.currentWebview.evalJS(`uniEvent(${JSON.stringify(info)})`);setInterval(() => {self.currentWebview.evalJS(`uniEvent(${JSON.stringify(info)})`);},1000)},methods: {onMessage(e) {console.log('来自页面的消息', e.detail.data);uni.showToast({title: e.detail.data, icon: 'none'});}}}
</script>
  • h5 页面代码
<!DOCTYPE html>
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>测试</title><script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
</head>
<body onload="addUniEvenPassthrough()"><div class="margin-top:200px;"></div><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><button onclick="sendMessage()">Send message to uniapp</button><script>function sendMessage() {uni.postMessage({data: 'Hello from web page!'});}function addUniEvenPassthrough() {window.uniEvent = function(info) {console.log('webview通信', JSON.stringify(info));}}</script>
</body>
</html>
  • 效果
    在这里插入图片描述
    在这里插入图片描述
http://www.lryc.cn/news/171555.html

相关文章:

  • Linux动态库
  • ESP-IDF学习——1.环境安装与hello-world
  • 【算法】二分答案
  • 阿曼市场最全开发攻略,看这一篇就够了
  • 探讨UUID和Secrets:确保唯一性与数据安全的利器
  • 06-Redis缓存高可用集群
  • LCP 18.早餐组合
  • Tomcat调优【精简版】
  • 通过NDK编译C程序运行在iMX6q开发板上
  • 【学习笔记】Java 一对一培训(2.1)Java基础语法
  • 外贸独立站哪家好?推荐的独立站建站平台?
  • 六、变量与常量
  • Fork() 函数:“父” 与 “子” 进程的交互(进程的创建)
  • JupyterNotebook设置Python环境的方法步骤
  • 腾讯云阿里云云服务器 Linux 操作系统 BT 宝塔面板快速建站教程
  • 【Linux】死锁理解
  • 基于Java所涉及的人工智能的框架
  • 【力扣】三角形最小路径和
  • 【Linux】指针常量和常量指针
  • LCP 22.黑白方格画
  • Java并发编程第8讲——ThreadLocal详解
  • 2023复旦大学计算机科学技术(网络空间安全)保研记录
  • linux系统通过docker安装python的jieba,如何找到jieba路径替换分词文件
  • Python Functions-函数
  • 【人工智能】机器学习的入门与提升
  • WEB漏洞原理之---【XMLXXE利用检测绕过】
  • element-table排序icon没有点亮
  • 传统的经典问题 Java 的 Interface 是干什么的
  • Linux 文件 目录管理
  • QT信号槽实现原理