需求:
先说一下情况,我们使用uniapp一套代码生成了Android APP、IOS APP、H5 三端应用,现有需求,当我APP中webview打开的网页,在APP内有对应的页面的时候,跳转到APP对应的页面浏览;
我们一般是APP中,通过webview打开一个第三方的地址(一般是个合集之类的网页),然后选择里边的资源,跳转到我们的H5网页中,但是webview浏览网页体验肯定不如APP直接浏览好,所以需要做一下跳转;
方案:
参考:
引入的 js.cdn.aliyun.dcloud.net.cn/dev/uni-app… 脚本,它这个地方是个坑。你要手动改源码。uni.webview.js文件里面注册的就是uni对象。被uniapp项目自带的uni对象覆盖了,我就是把源码里面注册uni对象换了个名字(webUni)就好了。
链接:pan.baidu.com/s/1PbLo2N_T… 提取码:4578
APP.vue
// 不存在则创建 JS 标签
let JumpScriptTag = document.getElementById('JumpScriptTag');
if(!JumpScriptTag) {
JumpScriptTag = document.createElement('script')
JumpScriptTag.type = 'text/javascript'
// 指定 JS 的地址
JumpScriptTag.src = '../../../static/data/uni.webview.js'
// 指定 JS 的 ID
JumpScriptTag.id = 'JumpScriptTag'
// 获取页面的 <head> 标签
let head = document.getElementsByTagName('head')[0]
// 将 JS 和 CSS 插入到 DOM 中
head.appendChild(JumpScriptTag);
}
document.addEventListener('UniAppJSBridgeReady', function() {
webUni.getEnv(function(res) {
// 判断环境只有是webview打开的时候才执行,这样避免了网页也触发一次
if(res.plus) {
let pathNameStr = window.location.pathname;
let searchStr = window.location.search;
if(pathNameStr == '/pages/user/index' ||
pathNameStr == '/pages/yigou/yigou' ||
pathNameStr == '/pages/bookshelf/shelf/shelf' ||
pathNameStr == '/pages/kecheng/kecheng' ||
pathNameStr == '/'
) {
webUni.switchTab({
url: pathNameStr
})
} else {
webUni.redirectTo({
url: pathNameStr + searchStr
})
}
}
});
});