在UniApp中使用WebView,实现双向通信

为什么用WebView?

WebView是[UniApp]中用于嵌入网页内容的组件,允许在应用中加载和显示网页。它适用于需要在应用中集成外部网页或HTML内容的场景,如展示帮助文档、加载第三方服务等。简单来说就是:我需要在app环境做一些uniapp的api不支持的功能,如:文件上传、音源转码等;

一、在[vue]文件中创建webview

插入的时候注意下,如果小伙伴用了自己src的地址发现页面出不来,可以也换成百度的试一下,如果百度的出的来,那就是你地址有问题;

官方文档

ini 复制代码
<template>
  <web-view
    ref="webview"
    id="myWebview"
    src="http://baidu.com"
    :fullscreen="false"
    @message="handleMessage"
    :webview-styles="{
      process: false,
    }" />
  <up-button @click="myClickFn">点击</up-button>
</template>
AI写代码html

二、[双向通信]:

2.1、uniapp→webview

  • vue中发送数据到html文件
javascript 复制代码
    // vue中发送数据到html文件,getMsgFromApp名字自定义
    myClickFn() {
      //  #ifdef APP-PLUS
      //选择到自己的webview-------h5不支持
      var currentWebview = this.$parent.$scope.$getAppWebview().children()[0]
      currentWebview.evalJS(`getMsgFromApp(${你的数据})`) 
      // #Endif
    },
AI写代码html
  • html中接收来自 uniapp 的消息
javascript 复制代码
    // html中接收来自 uniapp 的消息
    window.getMsgFromApp = function (arg) {
      console.log('接收来自 uniapp 的消息,arg',arg)
    }
AI写代码html

2.2、webview→uniapp

  • html向uniapp 发送消息
php 复制代码
    // 向 uniapp 发送消息
    function sendMessageToUniapp() {
      window.uni.postMessage({
        data: {
          type: 'fromWebview',
          message: '这是来自 webview 的消息',
        },
      })
    }
AI写代码html
  • uniapp接收html消息
typescript 复制代码
  <web-view
   、、、、
    @message="handleMessage"
    />    
 
    // 接收来自 webview 的消息
    handleMessage(event) {
      console.log('收到来自 webview 的消息:', event.detail)
    },
AI写代码html

三、完整代码

3.1UniApp:

xml 复制代码
<template>
  <web-view
    ref="webview"
    id="myWebview"
    src="http://baidu.com"
    :fullscreen="false"
    @message="handleMessage"
    :webview-styles="{
      process: false,
    }" />
  <up-button @click="myClickFn">点击</up-button>
</template>
<script>
export default {
  data() {
    return {}
  },
  methods: {
    // 接收来自 webview 的消息
    handleMessage(event) {
      console.log('收到来自 webview 的消息:', event.detail)
    },
    // 发送数据到html文件
    myClickFn() {
      //  #ifdef APP-PLUS
      //选择到自己的webview-------h5不支持
      var currentWebview = this.$parent.$scope.$getAppWebview().children()[0]
      currentWebview.evalJS(`getMsgFromApp(${你的数据})`) // #Endif
    },
  },
}
</script>
AI写代码html

3.2WebView的html:

xml 复制代码
<!DOCTYPE html>
<html>
 
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <title>我的webview</title>
</head>
 
<body>
  <script src="xxxxxxxxxx" charset="UTF-8"></script> //引入的js文件
  <script type="text/javascript" src="https://gitcode.net/dcloud/uni-app/-/raw/dev/dist/uni.webview.1.5.6.js"></script>
  <div id="content" class="content">
    内容!!!!!!
  </div>
 
  <script type="text/javascript">
    // 接收来自 uniapp 的消息
    window.getMsgFromApp = function (arg) {
      console.log('接收来自 uniapp 的消息')
    }
 
    // 向 uniapp 发送消息
    function sendMessageToUniapp() {
      window.uni.postMessage({
        data: {
          type: 'fromWebview',
          message: '这是来自 webview 的消息',
        },
      })
    }
 
    window.onload = function () {
      // 页面创建时执行
      console.log('页面创建了')
    }
 
    window.addEventListener('pagehide', function (event) {
      if (event.persisted) {
        // 页面被浏览器缓存(如iOS Safari的后台标签)
        console.log('页面被缓存');
      } else {
        // 页面正在被销毁
        console.log('页面被销毁')
      }
    });
  </script>
 
</body>
<style>
 
</style>
 
</html>
AI写代码html
相关推荐
qq_297574677 小时前
【实战教程】SpringBoot 集成阿里云短信服务实现验证码发送
spring boot·后端·阿里云
韩立学长9 小时前
【开题答辩实录分享】以《智能大学宿舍管理系统的设计与实现》为例进行选题答辩实录分享
数据库·spring boot·后端
编码者卢布11 小时前
【Azure Storage Account】Azure Table Storage 跨区批量迁移方案
后端·python·flask
她说..14 小时前
策略模式+工厂模式实现审批流(面试问答版)
java·后端·spring·面试·springboot·策略模式·javaee
梦梦代码精14 小时前
开源、免费、可商用:BuildingAI一站式体验报告
开发语言·前端·数据结构·人工智能·后端·开源·知识图谱
李慕婉学姐16 小时前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
tb_first16 小时前
SSM速通2
java·javascript·后端
一路向北⁢16 小时前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(一)
java·spring boot·后端·sse·通信
风象南16 小时前
JFR:Spring Boot 应用的性能诊断利器
java·spring boot·后端
爱吃山竹的大肚肚16 小时前
微服务间通过Feign传输文件,处理MultipartFile类型
java·spring boot·后端·spring cloud·微服务