uniAPP的webview封装使用

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
			});
		},
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值