uniapp通过webview调用H5页面

uniapp通过webview调用H5页面

使用uniapp开发小程序,需要从小程序跳转到H5页面,并且在H5页面可以返回小程序进行交互;对于该需求就选择了 webview 这个组件,具体怎么使用文档上也有详细描述,但在项目中还是需要验证;鉴于此,把自己在项目中事例整合出分享一下。

uniapp 官网上webview的描述,地址: https://uniapp.dcloud.io/component/web-view
微信官网上 webview 描述, 地址:https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html

为避免使用过程中多走弯路,现做以下几点温馨提示:

  1. tip:网页内 iframe 的域名也需要配置到域名白名单。
  2. tip:开发者工具上,可以在 web-view 组件上通过右键 - 调试,打开 web-view 组件的调试。
  3. tip:每个页面只能有一个 web-view,web-view 会自动铺满整个页面,并覆盖其他组件。
  4. tip:web-view 网页与小程序之间不支持除 JSSDK 提供的接口之外的通信。
  5. tip:在 iOS 中,若存在JSSDK接口调用无响应的情况,可在 web-view 的 src 后面加个#wechat_redirect解决。
  6. tip:避免在链接中带有中文字符,在 iOS 中会有打开白屏的问题,建议加一下 encodeURIComponent
一、在 pages 文件下创建 webview 文件,并在 pages.json 文件中,加入此文件路由
{
	"pages": [
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "首页"
			}
		}, 
		{
			"path": "pages/webview",    //加入 webview 文件
			"style": {
				"navigationBarTitleText": " "   //设置 title 为空,在 webview 文件中会动态改变 title 值
			}
		}
	]
}
二、在 webview 文件写入下面内容
<template>
	<view class="webview">
	  <web-view :src="url"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				 baseUrl: 'http://www.xxxx.com/' ,
				 url: ''
			} 
		},
		onLoad(op) {
			this.getWebview(op.id,op.type);
		},
		methods: {
			getWebviewPage(id,type){
				this.url= this.baseUrl +'?id='+id
				//设置 webview 界面的状态栏的 title
				if(type === '1'){
					uni.setNavigationBarTitle({
						title: 'XXXXXX'
					});
				}
			}
		}
	}
</script>

<style lang="less">
.webview {
	width: 100vw;
	height: 100vh;
}
</style>

三、在 小程序 页面写入跳转 webview 界面入口
<template>
	<view class="uni_index">
		<view v-if="!list.length" class="title">暂无数据</view>
		<view class="uni_index_contain" v-else>
			<view class="uni_index_list">
				<view class="uni_index_item" v-for="(item,index) in list" :key="item.uid" @click="goWebview(item.uid)">
					<view class="uni_index_item_title">{{item.title}}</view>
					<view class="uni_index_item_time">{{item.create_time}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

export default {
	data(){
		return {
			list:[],
		};
	},
	onLoad(){
		this.getData();
	},
	methods:{
		getData(){
			this.$request('/api/xxxx').then(res => {
				if (res.code == 0) {
					this.list = res.data;
				}
			});
		},
		goWebview(id){
			uni.navigateTo({
				url: '/pages/webview?id='+id+'&type=1'
			})
		}
	}
}
</script>

<style scoped>
	...............  //样式就不再描述了
</style>
四、H5 界面写法
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="referrer" content="no-referrer" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0, viewport-fit=cover"">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no">
  <script src="https://res.wx.qq.com/open/js/jweixin-1.3.0.js"></script> <!-- 引入微信js库 -->
</head>
<body>
  <div id="preview"></div>
  <button type="button" id="backBtn">返回</button>
</body>
<script>
	var url = 'http://www.xxxx.com'
    document.addEventListener('DOMContentLoaded',function(){
      var id = getQueryVariable('id'),
      		type = getQueryVariable('type'),
      		requestUrl = "/api/xxxx/detail/";
	      if(type === '1'){
	        document.title = 'xxxx';   //设置标题内容
	      }
	           
		  function getQueryVariable(variable){
			var query = window.location.search.substring(1);
			var getVariable= query.split("&");
			for (var i=0;i<getVariable.length;i++) {
			  var getVariableArr = getVariable[i].split("=");
			  if(getVariableArr[0] == variable){return getVariableArr[1];}
			}
			return(false);
		  }

		requestFun();
	    function requestFun(){
	      var xmlhttp;
	      if (window.XMLHttpRequest){
	        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
	        xmlhttp=new XMLHttpRequest();
	      }else{
	        // IE6, IE5 浏览器执行代码
	        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	      }
	      xmlhttp.onreadystatechange=function(){
	        if (xmlhttp.readyState==4 && xmlhttp.status==200){
	          var res = JSON.parse(xmlhttp.responseText);
	          document.getElementById('preview').innerText = res.data.content;
	        }
	      }
      
	      xmlhttp.open("POST",url+requestUrl,true);
	      xmlhttp.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
	      xmlhttp.send(JSON.stringify({id: id}));
	    }

	  document.getElementById('backBtn').addEventListener('click',function(){
		wx.miniProgram.navigateTo({   //跳转到小程序首页
			url:'/pages/index/index',
			success: function(){
				console.log('success')
			},
			fail: function(){
				console.log('fail');
			},
			complete:function(){
				console.log('complete');
			}
		  });
	  });
	  
	})
 </script>
</html>

以上就是完整的 webview 操作,部分有省略;如有什么疑问可在留言。

对你如果有用,请珍惜一下个人的劳动成功(纯手敲的),留下你的痕迹(点个赞喔)!

  • 26
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值