web-view.uvue 2.4 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2
<template>
	<view class="uni-flex-item">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
3 4
		<web-view ref="web-view" class="uni-flex-item" :src="src" :webview-styles="webview_styles" @message="message"
			@error="error" @loading="loading" @loaded="loaded">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
5
		</web-view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
6
		<view class="uni-padding-wrap uni-common-mt">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
7 8
			<view class="uni-input-v">
				<input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" />
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9
			</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
10 11 12 13 14 15 16
			<view class="uni-row uni-btn-v">
				<view class="uni-flex-item">
					<button type="primary" @click="back">后退</button>
				</view>
				<view class="uni-btn-ml uni-flex-item">
					<button type="primary" @click="forward">前进</button>
				</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
17
			</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18 19 20 21 22 23 24
			<view class="uni-row uni-btn-v">
				<view class="uni-flex-item">
					<button type="primary" @click="reload">重新加载</button>
				</view>
				<view class="uni-btn-ml uni-flex-item">
					<button type="primary" @click="stop">停止加载</button>
				</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
25
			</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
26 27 28
			<view class="uni-btn-v">
				<button type="primary" @click="nativeToWeb">原生和Web通信</button>
			</view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
29 30 31 32 33 34 35 36
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37
				src: 'https://www.baidu.com',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38 39 40 41
				webview_styles: {
					progress: {
						color: '#FF3333'
					}
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42
				}
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
			}
		},
		methods: {
			back() {
				(this.$refs['web-view'] as IWebViewNode).back();
			},
			forward() {
				(this.$refs['web-view'] as IWebViewNode).forward();
			},
			reload() {
				(this.$refs['web-view'] as IWebViewNode).reload();
			},
			stop() {
				(this.$refs['web-view'] as IWebViewNode).stop();
			},
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
58 59 60
			nativeToWeb() {
				(this.$refs['web-view'] as IWebViewNode).evalJS("alert('hello uni-app x')");
			},
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61
			message(event : WebViewMessageEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
62 63
				console.log(JSON.stringify(event.detail));
			},
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64
			error(event : WebViewErrorEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
65 66
				console.log(JSON.stringify(event.detail));
			},
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
67
			loading(event : WebViewLoadingEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
68 69
				console.log(JSON.stringify(event.type));
			},
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
70
			loaded(event : WebViewLoadedEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
71
				console.log(JSON.stringify(event.type));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
72 73 74 75 76 77 78 79
			},
			confirm(event : InputConfirmEvent) {
				console.log(event.detail.value);
				let url = event.detail.value;
				if (!url.startsWith('https://') && !url.startsWith('http://')) {
					url = 'https://' + url;
				}
				this.src = url;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
80 81 82 83 84 85
			}
		}
	}
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86 87 88
	.uni-input-v {
		padding: 10rpx 0;
	}
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
89

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
90 91 92
	.uni-btn-ml {
		margin-left: 10rpx;
	}
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
93
</style>