原生H5跳转uniapp开发的app方式:
1.在APP中使用web-view嵌入H5页面使用web-view组件实现
<template>
<view>
<web-view src="https://uniapp.dcloud.net.cn/component/web-view.html"></web-view>
</view>
</template>
2.在H5项目引入webview.js
<!-- 需要把 uni.webview.1.5.4.js 下载到自己的服务器 --> 导入web-view 的SDK
<script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js"></script>
3.调用方法跳转至app 内部的页面的关键方法 uni.reLaunch
uni.reLaunch({
url: '/pages/tabBar/component/component'
});
4.跳转方式:
<!-- uni 的 SDK -->
<!-- 需要把 uni.webview.1.5.4.js 下载到自己的服务器 -->
<script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js"></script>
<script type="text/javascript">
// 待触发 `UniAppJSBridgeReady` 事件后,即可调用 uni 的 API。
//H5端与app端通信
document.addEventListener('UniAppJSBridgeReady', function() {
uni.postMessage({
data: {
action: 'message'
}
});
uni.getEnv(function(res) {
console.log('当前环境:' + JSON.stringify(res));
});
document.querySelector('.btn-list').addEventListener('click', function(evt) {
var target = evt.target;
if (target.tagName === 'BUTTON') {
var action = target.getAttribute('data-action');
switch (action) {
case 'switchTab':
uni.switchTab({
url: '/pages/tabBar/API/API'
});
break;
case 'reLaunch':
uni.reLaunch({
url: '/pages/tabBar/component/component'
});
break;
case 'navigateBack':
uni.navigateBack({
delta: 1
});
break;
default:
uni[action]({
url: '/pages/component/button/button'
});
break;
}
}
});
document.getElementById('postMessage').addEventListener('click', function() {
uni.postMessage({
data: {
action: 'message'
}
});
});
});
</script>
上面的跳转方式可以解决APP嵌入原生H5页面跳转回APP的问题,但是使用uniapp开发的H5就会失效,在uniapp开发H5页面使用 uni.reLaunch 只能跳转到H5项目本地的页面。
uniapp开发H5跳转方式:
1.在H5项目里引入 web-view.js:
通过官网链接下载 web-view.js文件(下载链接,打开链接右键另存为下载链接,),放到项目的static/js文件中
2.在main.js中引入
import App from './App'
import webView from './static/js/uni.webview.1.5.2.js'
....
3.使用方式:
// #ifdef APP-PLUS
uni.webView.postMessage({
data: {
action: 'uni-app',
}
});
uni.webView.reLaunch({
url: '/pages/tabBar/component/component'
});
区别在于 uniapp开发的H5要在uni前面加上 webView
项目中使用的完整代码创建示例:
- 在app项目添加webview文件夹添加gridWebview.vue文件。代码如下
<template>
<view>
<web-view :src="src" @message="getMessage" ></web-view>
</view>
</template>
<script>
export default {
data() {
return {
src: '',
}
},
computed: {
token() {
return this.$store.state.login.token || ''
},
roles() {
return this.$store.state.login.roles || ''
},
userInfo(){
return this.$store.state.login.userInfo || ''
}
},
onLoad(opt) {
console.log(opt)
//拼接所需参数
this.title = opt.title
this.src = opt.src
+ (opt.src.includes('?') ? '&' : '?')
+ (this.token.length>0 ? 'token=' + this.token : '')
+ (this.userInfo.phone.length>0 ?`&phone=${this.userInfo.phone}` :'' )
+ (this.userInfo.userId.length>0 ?`&userId=${this.userInfo.userId}` :'' )
+ (this.userInfo.username.length>0 ?`&username=${this.userInfo.username}` :'' )
+ (opt.data?`&data=${opt.data}` :'' )
+ (opt.cityid?`&cityid=${opt.cityid}` :'' )
},
onShow() {
// #ifdef APP-PLUS
设置webview显示时顶部丢失问题
var height = 0; //定义动态的高度变量,如高度为定值,可以直接写
uni.getSystemInfo({
//成功获取的回调函数,返回值为系统信息
success: (sysinfo) => {
height = sysinfo.windowHeight -
40; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种,如果不减,页面会下沉,看不到底部导航栏
},
complete: () => {}
});
var currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
setTimeout(function() {
var wv = currentWebview.children()[0];
wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为px
top: 40, //此处是距离顶部的高度,应该是你页面的头部
height: height, //webview的高度
scalable: false, //webview的页面是否可以缩放,双指放大缩小,
})
}, 500); //如页面初始化调用需要写延迟
// #endif
},
methods: {
customBack() {
// 在Webview页面中调用uni.navigateBack()
uni.navigateBack();
},
getMessage(event) { //在H5端使用通信返回App端
console.log(event, '0000000000000000000000000')
if (event.detail.data[0].action == 'uni-app') {
uni.navigateBack();
}
}
},
}
</script>
2. app跳转H5使用如下方法:
//跳转到对应的webview页面并传入url和其他参数
uni.navigateTo({
url: item.url.startsWith('http') ? `/pages/webview/gridWebview?title=${item.name}&src=${item.url}` : item.url
})
3.H5返回app使用如下方法:
app.vue页面需要引入uni.webview.1.5.2.js文件 (目前此方法只能在真机上返回,H5上目前点击无效)
<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
onLaunch: function() {
this.initScript()
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
methods: {
initScript() {
var script = document.createElement('script');
script.type = "text/javascript";
script.async = true;
script.src = "./static/js/uni.webview.1.5.2.js";
document.head.appendChild(script);
console.log(wx, "wx");
}
}
在需要返回的页面添加返回方法:
goBack() {
console.log('0000000', uni, )
uni.webView.postMessage({
data: {
action: 'uni-app', //可自己随意定义,需要与app中接收通信的方法对应
}
});
}
官方H5代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>网络网页</title>
<style type="text/css">
.btn {
display: block;
margin: 20px auto;
padding: 5px;
background-color: #007aff;
border: 0;
color: #ffffff;
height: 40px;
width: 200px;
}
.btn-red {
background-color: #dd524d;
}
.btn-yellow {
background-color: #f0ad4e;
}
.desc {
padding: 10px;
color: #999999;
}
.post-message-section {
visibility: hidden;
}
</style>
</head>
<body>
<p class="desc">web-view 组件加载网络 html 示例。点击下列按钮,跳转至其它页面。</p>
<div class="btn-list">
<button class="btn" type="button" data-action="navigateTo">navigateTo</button>
<button class="btn" type="button" data-action="redirectTo">redirectTo</button>
<button class="btn" type="button" data-action="navigateBack">navigateBack</button>
<button class="btn" type="button" data-action="reLaunch">reLaunch</button>
<button class="btn" type="button" data-action="switchTab">switchTab</button>
</div>
<div class="post-message-section">
<p class="desc">网页向应用发送消息,注意:小程序端应用会在此页面后退时接收到消息。</p>
<div class="btn-list">
<button class="btn btn-red" type="button" id="postMessage">postMessage</button>
</div>
</div>
<script type="text/javascript">
var userAgent = navigator.userAgent;
if (userAgent.indexOf('AlipayClient') > -1) {
// 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。
document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>');
} else if (/QQ/i.test(userAgent) && /miniProgram/i.test(userAgent)) {
// QQ 小程序
document.write(
'<script type="text/javascript" src="https://qqq.gtimg.cn/miniprogram/webview_jssdk/qqjssdk-1.0.0.js"><\/script>'
);
} else if (/miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent)) {
// 微信小程序 JS-SDK 如果不需要兼容微信小程序,则无需引用此 JS 文件。
document.write('<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"><\/script>');
} else if (/toutiaomicroapp/i.test(userAgent)) {
// 头条小程序 JS-SDK 如果不需要兼容头条小程序,则无需引用此 JS 文件。
document.write(
'<script type="text/javascript" src="https://s3.pstatp.com/toutiao/tmajssdk/jssdk-1.0.1.js"><\/script>');
} else if (/swan/i.test(userAgent)) {
// 百度小程序 JS-SDK 如果不需要兼容百度小程序,则无需引用此 JS 文件。
document.write(
'<script type="text/javascript" src="https://b.bdstatic.com/searchbox/icms/searchbox/js/swan-2.0.18.js"><\/script>'
);
} else if (/quickapp/i.test(userAgent)) {
// quickapp
document.write('<script type="text/javascript" src="https://quickapp/jssdk.webview.min.js"><\/script>');
}
if (!/toutiaomicroapp/i.test(userAgent)) {
document.querySelector('.post-message-section').style.visibility = 'visible';
}
</script>
//跳转方式,下面一部分代码
<!-- uni 的 SDK -->
<!-- 需要把 uni.webview.1.5.4.js 下载到自己的服务器 -->
<script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js"></script>
<script type="text/javascript">
// 待触发 `UniAppJSBridgeReady` 事件后,即可调用 uni 的 API。
// H5端与app端通信
document.addEventListener('UniAppJSBridgeReady', function() {
uni.postMessage({
data: {
action: 'message'
}
});
uni.getEnv(function(res) {
console.log('当前环境:' + JSON.stringify(res));
});
document.querySelector('.btn-list').addEventListener('click', function(evt) {
var target = evt.target;
if (target.tagName === 'BUTTON') {
var action = target.getAttribute('data-action');
switch (action) {
case 'switchTab':
uni.switchTab({
url: '/pages/tabBar/API/API'
});
break;
case 'reLaunch':
uni.reLaunch({
url: '/pages/tabBar/component/component'
});
break;
case 'navigateBack':
uni.navigateBack({
delta: 1
});
break;
default:
uni[action]({
url: '/pages/component/button/button'
});
break;
}
}
});
document.getElementById('postMessage').addEventListener('click', function() {
uni.postMessage({
data: {
action: 'message'
}
});
});
});
</script>
</body>
</html>