uniapp的webview实现左滑返回上一个页面操作方法

 更新时间:2023年12月12日 15:36:46   作者:linlinlove2  
uniapp默认左滑是关闭整个webview,而不是关闭当前页,本文给大家介绍uniapp的webview实现左滑返回上一个页面操作方法,感兴趣的朋友一起看看吧

uniapp默认左滑是关闭整个webview,而不是关闭当前页 实现思路:拦截webview的url跳转操作,将新url用webview组件重新打开,当左滑的时候,默认关闭的就是当前webview,继而跳转到上一次的页面中

<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参数是:", option.weburl); //打印出上个页面传递的参数。
			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,手机返回键处理成返回上一页,上一个网页

加webview的vue相关处写如下加红代码

<script>
const facebook = uni.requireNativePlugin('sn-facebook');
var wv; //计划创建的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); //如果是页面初始化调用时,需要延时一下
        // #endif
    },
    methods: {
        onMessage({ detail }) {
            const data = detail.data[0];
            console.log('onMessage', data);
            if (data.action == 'login') {
                // 登录:自定义参数
                facebook.loginWithParams(
                    {
                        permissions: [
                            // 权限,更多权限请看 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>

到此这篇关于uniapp的webview实现左滑返回上一个页面的文章就介绍到这了,更多相关uniapp webview左滑返回内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论