uniapp上使用webview与浏览器交互,支持三端(android、iOS、harmonyos next)

效果展示:

*这波绝对是走在最前面。uniapp上使用webview与浏览器交互,支持三端(android、iOS、harmonyos next)。还有部分人用的vue2的语法,目前是最新的vue3写法。废话不多说,直接上代码:

uniapp代码(app端)

javascript 复制代码
<template>
	<web-view
		id="webviewId"
		ref="webviewRef"
		:src="'https://static-mp-ff40e3ac-9cb8-4dc9-add1-486c5e802f30.next.bspapp.com/ywx/webview.html'"
		@message="messageHanle"></web-view>
</template>
<script setup>
import { ref, onMounted, getCurrentInstance } from "vue";

const webviewRef = ref(null);
// #ifdef APP-HARMONY
const context = getCurrentInstance(); // 允许访问对高级使用或库创建者有用的内部组件实例
const webwiewCtx = uni.createWebviewContext("webviewId", context.proxy);
// #endif

// 发送数据到网页
const sendDataToWeb = (data) => {
	// #ifndef APP-HARMONY
	// 获取所有页面路由
	const pages = getCurrentPages();
	// 找到最后一个索引
	const index = pages.length - 1;
	// 获取到当前页面的webView实例
	const currentWebview = pages[index].$getAppWebview();
	// 取到真正的webview
	const webView = currentWebview.children()[0];
	console.log(webView);
	webView.evalJS(`RequireMapMobile('${JSON.stringify(data)}')`);
	// #endif
	
	// #ifdef APP-HARMONY
	setTimeout(()=>{
		webwiewCtx.evalJS(`RequireMapMobile('${JSON.stringify(data)}')`);
	}, 200)
	// #endif
};

// 接收浏览器发送的消息
const messageHanle = (e) => {
	console.log("浏览器发送的消息", e.detail.data[0]);
};

html代码(浏览器端)

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .send{
            width: 100%;
            height: 100vh;
            background-color: #ebebeb;
            text-align: center;
            font-size: 18px;
            font-weight: bold;
        }
        .send .content{
            display: block;
            line-height: 100px;
        }
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <div class="send">
        <span class="content">大家好,我是浏览器!</span>
    </div>
</body>
<script type="text/javascript" src="./uni.webview.js"></script>
<script>
    window.RequireMapMobile = function(val){
        console.log('来自App发送的数据', JSON.stringify(val));
        // 修改class为send的背景颜色
        document.querySelector('.content').style.color = 'gold';
    }
    setTimeout(() => {
        uni.postMessage({
            data: {
                message: '我是浏览器发送的数据!'
            }
        });
        // 修改class为send的背景颜色
        document.querySelector('.send').style.backgroundColor = 'pink';
    }, 500);
</script>
</html>

有任何问题直接评论区留言。欢迎大家共同讨论uniapp、纯血鸿蒙相关话题。

相关推荐
计算机学姐6 分钟前
基于SpringBoot的在线学习网站平台【个性化推荐+数据可视化+课程章节学习】
java·vue.js·spring boot·后端·学习·mysql·信息可视化
csj507 分钟前
安卓基础之《(26)—回到桌面与切到任务列表》
android
特立独行的猫a10 分钟前
HarmonyOS鸿蒙PC的QT应用开发:(二、开发环境搭建及第一个HelloWorld)
qt·华为·harmonyos·鸿蒙·鸿蒙pc
Grackers10 分钟前
Android Perfetto 系列 7:MainThread 和 RenderThread 解读
android
浮芷.11 分钟前
Flutter 框架跨平台鸿蒙开发 - 旧物改造灵感库应用
科技·flutter·华为·harmonyos·鸿蒙
暴力袋鼠哥11 分钟前
基于 Django 与 Vue 的汽车数据分析系统设计与实现
vue.js·django·汽车
whatever who cares19 分钟前
java/android中单例模式详解
android·java
草莓熊Lotso23 分钟前
【Linux系统加餐】 mmap 文件映射全解:从底层原理、API 到实战开发(含 malloc 模拟实现)
android·linux·运维·服务器·c语言·c++
peakmain926 分钟前
CmComposeUI——基于 Kotlin Multiplatform Compose 的 UI 组件库
android
喵了几个咪30 分钟前
Headless 架构优势:内容与展示解耦,一套 API 打通全端生态
vue.js·架构·golang·cms·react·taro·headless