uniapp App页面通过 web-view 调用网页内方法

先是报这个错

A parser-blocking, cross site (i.e. different eTLD+1) script, https://api.map.baidu.com/getscript?v=3.0&ak=pgJsRF87Fjia&services=&t=20250225111334, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details. at https://api.map.baidu.com/api?v=3.0&ak=p1Ona:1
17:44:49.105 [WARNING] :A parser-blocking, cross site (i.e. different eTLD+1) script, https://api.map.baidu.com/getscript?v=3.0&ak=p1O7Fjia&services=&t=20250225111334, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details. at https://api.map.baidu.com/api?v=3.0&ak=p1On6Mpgs8yRrLU7RRgJsRF87Fjia:1
17:44:49.274 [HMR] Waiting for update signal from WDS... at http://192.168.1.95:9091/static/js/chunk-vendors.js:15242
17:44:49.790 Download the Vue Devtools extension for a better development experience:
https://github.com/vuejs/vue-devtools at http://192.168.1.95:9091/static/js/chunk-vendors.js:49249

排查发现可能是 

https://api.map.baidu.com/getscript?v=3.0&ak=p1On6Mpgs8yRR7U7RRgJsRF87Fjia&services=&t=20250225111334

这个js加载太慢 终止了 document.write 调用的js,把这个js文件引入先去掉就可以了

<template>
	<view>
		<button  @click="sendMessage" style="z-index: 999999;height: 50px;">Send Message</button>
		<web-view ref="webview" src="http://192.168.1.95:9091/#/pages/index" style="height: 400px;margin-top: 50px;"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		methods: {
			sendMessage() {
				//this.$refs.webview.evalJs(`alert('Message from UniApp')`);
				const currentWebview = this.$scope.$getAppWebview();
				const wv = currentWebview.children()[0];
				wv.evalJS(`alert('Message from UniApp')`);
			},
		},
		onReady() {
		    // 确保 web-view 已加载完成
		    setTimeout(() => {
		      this.sendMessage();
		    }, 3000);
		  },
	}
</script>

<style>

</style>

### 如何在 UniApp 中打包应用程序以包含 `web-view` 组件 #### 准备工作 为了确保能够顺利集成并使用 `web-view` 组件,在开发前需确认已安装最新版本的 HBuilderX IDE 及其相关插件[^1]。 #### 创建项目结构 创建一个新的 UniApp 项目,按照官方文档指导完成基础配置。对于计划内嵌 Web 页面的应用场景,建议提前规划好本地文件路径以及远程 URL 的管理方式[^2]。 #### 添加 web-view 组件 在页面布局中引入 `<web-view>` 标签来加载外部网页或本地 HTML 文件: ```html <template> <view class="container"> <!-- 使用 src 属性指定要加载的内容 --> <web-view :src="url"></web-view> </view> </template> <script setup lang="ts"> import { ref } from 'vue'; // 定义变量存储目标URL const url = ref('https://example.com'); </script> ``` #### 处理跨平台差异 由于不同操作系统对 WebView API 支持存在细微差别,特别是在 nVue 场景下获取 webView 实例的方法有所不同,因此需要针对特定环境编写适配逻辑: - 对于标准 Vue 页面可以直接调用 uni.getWebViewById 获取实例; - 而对于 nVue 则应采用 plus.webview.currentWebview().opener() 方法获得父级 WebView 引用。 #### 数据交互机制 当涉及到父子组件间的数据交换时,可以利用 postMessage / onMessage 接口实现双向通信功能。如果遇到事件监听器未响应的情况,请检查是否正确设置了消息处理函数,并注意区分 iOS/Android 平台特有的行为特性。 #### 构建与发布流程 最后一步就是执行构建命令将整个工程编译为目标移动应用格式 (APK/IPA),期间务必遵循官方指南调整 build.json 配置项中的参数设置,比如 appid、name 等基本信息,还有关于权限声明的部分也非常重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值