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>
相关推荐
袁煦丞36 分钟前
【局域网秒传神器】LocalSend:cpolar内网穿透实验室第418个成功挑战
前端·程序员·远程工作
江城开朗的豌豆37 分钟前
Vuex数据突然消失?六招教你轻松找回来!
前端·javascript·vue.js
好奇心笔记1 小时前
ai写代码随机拉大的,所以我准备给AI出一个设计规范
前端·javascript
江城开朗的豌豆1 小时前
Vue状态管理进阶:数据到底是怎么"跑"的?
前端·javascript·vue.js
用户21411832636021 小时前
dify案例分享-Dify v1.6.0 重磅升级:双向 MCP 协议引爆 AI 生态互联革命
前端
程序员海军1 小时前
AI领域又新增协议: AG-UI
前端·openai·agent
我想说一句1 小时前
React待办事项开发记:Hook魔法与组件间的悄悄话
前端·javascript·前端框架
真夜1 小时前
CommonJS与ESM
前端·javascript
LaoZhangAI1 小时前
GPT-image-1 API如何传多图:开发者完全指南
前端·后端
G等你下课1 小时前
从点击到执行:如何优雅地控制高频事件触发频率
前端·javascript·面试