uniapp 引入h5页面

如何在app中引入网页?通过使用webview 标签

  • uniapp 官网给出的webview使用方法:
    https://uniapp.dcloud.io/component/web-view
    但是给出的基本都是在app中使用原生html页面的方法和demo,但是用vue写的H5页面就不能正常使用,下面是解决方法:
第一步:在app项目中新建webview页面(src为你自己的网页地址)
js 复制代码
<template>
    <view>
        <web-view :webview-styles="webviewStyles" src="https://uniapp.dcloud.io/static/web-view.html" @message="getMessage"></web-view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                webviewStyles: {
                    progress: {
                        color: '#FF3333'
                    }
                }
            }
        },
        methods: {
		    getMessage(event) {
		      console.log(event);
		      uni.showModal({
		        content: JSON.stringify(event.detail),
		      });
		  }
	  }
    }
</script>

<style>

</style>
第二步:在你的H5页面引入 https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js 然后在你的H5页面进行使用,切记,使用里面的API一定要加上webView,例如(uni.webView.postMessage({});更多相关的API在官网里面自行查找,代码如下
js 复制代码
<template>
	<view class="w-full" style="background-color: #fafbfa;">
		<view @click = messageApp>点击发送消息</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			
		};
	},
	computed: {},
	onShow() {},
	methods: {
		messageApp(){
			uni.webView.postMessage({
				data: {
					data: '这是向app发送的消息',
					type: 'message'
				}
			});
		}
	}
};
</script>

<style></style>
相关推荐
spmcor2 天前
身份证读卡“无感登录”方案实践:从手动点击到自动检测
uni-app
PedroQue992 天前
uni-router v1.8.0新增冷启动守卫补执行
前端·uni-app
PedroQue993 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app
一份执念5 天前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
PedroQue995 天前
V1.6.1性能优化:高频路径提速与代码精简
前端·uni-app
夏碧笔7 天前
uni-app跨端地图实战:用第三方LBS替代微信平台收费服务
uni-app
用户69903048487512 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_12 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Geek_Vison12 天前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
CHB13 天前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos