uniapp的webview實現(xiàn)左滑返回上一個頁面操作方法
uniapp默認左滑是關(guān)閉整個webview,而不是關(guān)閉當前頁 實現(xiàn)思路:攔截webview的url跳轉(zhuǎn)操作,將新url用webview組件重新打開,當左滑的時候,默認關(guān)閉的就是當前webview,繼而跳轉(zhuǎn)到上一次的頁面中
<template>
<view>
<web-view :src="weburl" :update-title="false" :webview-styles="webviewStyles">
</web-view>
</view>
</template>
<script>
export default {
data() {
return {
// 進度條
webviewStyles: {
progress: {
color: '#FF3333'
}
},
weburl: ""
};
},
onLoad(option) {
console.log("接收到的url參數(shù)是:", option.weburl); //打印出上個頁面?zhèn)鬟f的參數(shù)。
this.weburl = option.weburl
},
onReady() {
var pages = getCurrentPages();
var page = pages[pages.length - 1];
var currentWebview = page.$getAppWebview();
var url = currentWebview.children()[0].getURL();
console.log('=== url ===', url);
var wv = currentWebview.children()[0];
wv.overrideUrlLoading({
mode: 'reject',
match: '.*'
}, function(e) {
console.log('reject url: ' + e.url);
uni.navigateTo({
url: `/pages/webbox/webbox?weburl=${e.url}`
})
});
},
onBackPress(e) {
let pages = getCurrentPages()
let page = pages[pages.length - 1];
let currentPages = page.$getAppWebview()
currentPages.close()
return false
},
onNavigationBarButtonTap() {
console.log("點擊了標題欄按鈕")
uni.$emit("showMenu")
},
methods: {}
}
</script>
<style lang="scss">
</style>uniapp webview 加載H5,手機返回鍵處理成返回上一頁,上一個網(wǎng)頁
加webview的vue相關(guān)處寫如下加紅代碼

<script>
const facebook = uni.requireNativePlugin('sn-facebook');
var wv; //計劃創(chuàng)建的webview
export default {
data() {
return {
canBack: false
};
},
onLoad() {},
onBackPress() {
if (wv && this.canBack) {
wv.back();
return true;
}
return false;
},
onReady() {
// #ifdef APP-PLUS
var self = this;
var currentWebview = this.$scope.$getAppWebview(); //此對象相當于html5plus里的plus.webview.currentWebview()。在uni-app里vue頁面直接使用plus.webview.currentWebview()無效,非v3編譯模式使用this.$mp.page.$getAppWebview()
setTimeout(function() {
wv = currentWebview.children()[0];
wv.addEventListener(
'progressChanged',
function(e) {
wv.canBack(function(e) {
self.canBack = e.canBack;
});
},
false
);
}, 500); //如果是頁面初始化調(diào)用時,需要延時一下
// #endif
},
methods: {
onMessage({ detail }) {
const data = detail.data[0];
console.log('onMessage', data);
if (data.action == 'login') {
// 登錄:自定義參數(shù)
facebook.loginWithParams(
{
permissions: [
// 權(quán)限,更多權(quán)限請看 https://developers.facebook.com/docs/permissions/reference
'email',
'public_profile'
],
// 返回字段,更多字段請查看 https://developers.facebook.com/docs/graph-api/reference/user/
fields: 'id, name, email'
},
e => {
console.log(e);
this.postMessage({ ...e, action: 'loginCallback' });
}
);
}
},
// postMessage to web
postMessage(data) {
if (wv) {
wv.evalJS('window.postMessage(' + JSON.stringify(data) + ');');
} else {
uni.showToast({
icon: 'none',
title: 'webview不存在'
});
}
}
}
};
</script>到此這篇關(guān)于uniapp的webview實現(xiàn)左滑返回上一個頁面的文章就介紹到這了,更多相關(guān)uniapp webview左滑返回內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Aptana Studio生成自動備份文件的解決辦法
關(guān)于Aptana Studio生成自動備份文件的解決辦法2009-12-12
JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼
這篇文章主要介紹了JavaScript中l(wèi)ayim之整合右鍵菜單的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

