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>
相关推荐
2401_878454531 小时前
Themeleaf复用功能
前端·学习
葡萄城技术团队3 小时前
基于前端技术的QR码API开发实战:从原理到部署
前端
八了个戒4 小时前
「数据可视化 D3系列」入门第三章:深入理解 Update-Enter-Exit 模式
开发语言·前端·javascript·数据可视化
noravinsc5 小时前
html页面打开后中文乱码
前端·html
小满zs5 小时前
React-router v7 第四章(路由传参)
前端·react.js
小陈同学呦6 小时前
聊聊双列瀑布流
前端·javascript·面试
键指江湖6 小时前
React 在组件间共享状态
前端·javascript·react.js
诸葛亮的芭蕉扇7 小时前
D3路网图技术文档
前端·javascript·vue.js·microsoft
小离a_a7 小时前
小程序css实现容器内 数据滚动 无缝衔接 点击暂停
前端·css·小程序
徐小夕7 小时前
花了2个月时间研究了市面上的4款开源表格组件,崩溃了,决定自己写一款
前端·javascript·react.js