uniAPP的webview封装使用

本文介绍了如何在uniAPP中封装webview组件,并分别展示了在微信小程序和App上的使用方法。在微信小程序中,通过vue模板和uni.setNavigationBarTitle设置页面标题;在App中,利用App-PLUS条件编译设置webview样式和标题。同时提供了一个名为goToWebView的函数,用于跳转到webview页面并传递参数。
摘要由CSDN通过智能技术生成

uniAPP的webview封装使用

一般分为微信小程序的和App的,微信小程序、H5使用.vue文件,运行时到App上会自动采用.nvue,
不采用.nvue文件时,在App中html页面的标题会自动填充到系统标题栏
在这里插入图片描述

  1. 微信小程序
<template>
	<view>
		<view><web-view :src="path"></web-view></view>
	</view>
</template>

<script>
import { isNotEmpty } from '../../utils/util.js';
export default {
	data() {
		return {
			path: '',
			title: ''
		};
	},
	onLoad: function(option) {
		this.path = decodeURIComponent(option.path);//注意转码
		this.title = option.title;
	},
	onReady() {
		if (isNotEmpty(this.title)) {//isNotEmpty非空判断
			console.log(this.title);
			this.setTitle(this.title);
		}
	},
	methods: {
		setTitle(title) {//设置标题
			uni.setNavigationBarTitle({
				title: title
			});
		}
	}
};
</script>

<style></style>

  1. App
<template>
	<view class="content"><web-view :src="path" class="web"></web-view></view>
</template>

<script>
import { isNotEmpty } from '../../utils/util.js';
export default {
	data() {
		return {
			path: '',
			title: ''
		};
	},
	onLoad: function(option) {
		this.path = decodeURIComponent(option.path);//注意转码
		this.title = option.title;
	},
	onReady() {
		if (isNotEmpty(this.title)) {
			this.setTitle(this.title);
		}
		// #ifdef APP-PLUS
		var currentWebview = this.$mp.page.$getAppWebview(); //获取当前页面的webview对象
		setTimeout(function() {
			wv = currentWebview.children()[0];
			wv.setStyle({ scalable: true });
		}, 1000); //如果是页面初始化调用时,需要延时一下
		// #endif
	},
	methods: {
		setTitle(title) {
			uni.setNavigationBarTitle({
				title: title
			});
		}
	}
};
</script>

<style>
.content {
	flex: 1;
	flex-direction: column;
}
.web {
	width: 750rpx;
	flex: 1;
}
</style>

使用

goToWebView(path, title) {
			uni.navigateTo({//注意转码
				url: '../../webview/webview?path=' + encodeURIComponent(path) + '&title=' + title
			});
		},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值