首页 > 建站教程 > APP开发,混合APP >  uni-app打包app时出现:‘We're sorry...’的错误页面正文

uni-app打包app时出现:‘We're sorry...’的错误页面

uni-app打包app时出现:‘We're sorry...’的错误页面,当webview组件(窗口)加载错误地址(如本地页面不存在)或者访问网络资源失败(如无法访问网络)时会自动显示默认错误页面:

uni-app打包app时出现:‘We're sorry...’的错误页面

如需修改默认错误页面样式,可以通过以下方法自定义Webview的404等错误页面。


设置自定义错误页面

打开项目的manifest.json文件,在 “App常用其它设置” -> “自定义404错误页面” 下点击 “浏览” 选择页面文件:

uni-app自定义错误页面

注:建议使用html文件,并放到项目根目录下的 hybrid/html 文件夹中


错误页面中监听事件

自定义404错误页面时,可能需要在 error.html 页面中获取错误原因,可以通过以下方法监听 "error" 事件获取完整错误信息,示例如下:

// 获取错误信息
document.addEventListener("error", function(e){
  var url = e.url;  // 错误页面的url地址
  var href = e.href; // 错误页面的完整路径(包括完整的协议头)
},false);


error.html错误页面代码参考:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="HandheldFriendly" content="true" />
<meta name="MobileOptimized" content="320" />
<title>Error</title>
<style>
	* {
		-webkit-user-select: none;
	}
	html,
	body {
		margin: 0px;
		padding: 0px;
		width: 100%;
		height: 100%;
		text-align: center;
		word-break: break-all;
		-webkit-touch-callout: none;
		-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	}
	.button {
		width: 60%;
		font-size: 18px;
		font-weight: normal;
		text-decoration: none;
		text-align: center;
		padding: .5em 0em;
		margin: .5em auto;
		color: #333333;
		border-radius:22px;
	/* 	background-color: #EEEEEE;
		border: 1px solid #CCCCCC;
		-webkit-border-radius: 5px;
		border-radius:22px; */
	}
	.button:active {
		background-color: #CCCCCC;
	}
	.back{
		color: #6767EA;
		border: 1px solid #6767EA;
	}
	.reload{
		color: #fff;
		border: none;
		background: linear-gradient(to right, #6767EA, #75ACFF);
		margin-left: 60upx;
	}
</style>
</head>
<body>
<div style="width:100%;height:20%;"></div>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="84px" height="94px" viewBox="0 0 84 94" enable-background="new 0 0 84 94" xml:space="preserve">  <image id="image0" width="84" height="94" x="0" y="0"
		href="你的svg" />
</svg>
<p style="font-size:18px;font-weight:bolder;">抱歉,没有找到您请求的页面</p>
<p id="info" style="font-size:12px;"></p>
<p id="errorInfo" style="font-size:12px;"></p>
<!--<div class="button" onclick="history.back()">Retry</div>-->
<div class="button back" onclick="backClick()">返回</div>
<div class="button back" onclick="closeClick()">关闭</div>
<div class="button reload" onclick="restartClick()">刷新</div>
<script type="text/javascript">
	var ab_ = {
		plusReady(callback) {
			if (window.plus) {
				callback()
			} else {
				document.addEventListener('plusready', callback)
			}
		}
	}
	ab_.plusReady(function() {
		var native
		var center
		try {
			native = plus.os.name.toLowerCase()
			if (native === 'android') {
				center = plus.android.importClass("com.absir.uniplugin.AbCenter").ME()
			} else if (plus.ios) {
				center = plus.ios.importClass("AbCenter").ME()
			}
		} catch (e) {
			console.error(e)
		}
		ab_.native = native
		ab_.center = center
		ab_.postEvent = function(event, paras) {
			if (center) {
				if (native === 'android') {
					center.postEvent(event, paras)
				} else {
					plus.ios.invoke(center, 'postEvent:paras:', event, paras)
				}
				return true
			}
		}
		plus.key.addEventListener('backbutton', function() {
			backClick()
		}, false)
	})
	function backClick() {
		(history.length == 1) && closeClick();
		var c = setTimeout(function() {
			closeClick();
		}, 1000);
		window.onbeforeunload = function() {
			clearTimeout(c);
		}
		history.go(-2);
	}
	function closeClick() {
		ab_.postEvent('message', 'backProj')
	}
	function restartClick() {
		plus.runtime.restart()
	}
	
document.addEventListener("error", function(e){
	var url = e.url;  // 错误页面的url地址
	var href = e.href; // 错误页面的完整路径(包括完整的协议头)
	if(url || href) {
		document.querySelector('#errorInfo').innerHTML = '错误信息:'+(url || href)
	}
},false)
</script>
</body>
</html>