web-view-local.uvue 2.2 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<template>
	<view class="uni-flex-item">
		<web-view ref="web-view" class="uni-flex-item" src="/hybrid/html/local.html" @onPostMessage="onPostMessage"
			@error="error" @loading="loading" @loaded="loaded">
		</web-view>
		<view class="uni-btn-v">
			<button type="primary" @click="back">后退</button>
		</view>
		<view class="uni-btn-v">
			<button type="primary" @click="forward">前进</button>
		</view>
		<view class="uni-btn-v">
			<button type="primary" @click="reload">重新加载</button>
		</view>
		<view class="uni-btn-v">
			<button type="primary" @click="stop">停止加载</button>
		</view>
	</view>
</template>

<script>
	import JSONObject from 'com.alibaba.fastjson.JSONObject';
	export default {
		data() {
			return {

			}
		},
		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();
			},
			onPostMessage(event: WebViewPostMessageEvent) {
				console.log(JSON.stringify(event.detail));
				const data = event.detail.data as JSONObject;
				switch (data['action']) {
					case 'navigateTo':
						uni.navigateTo({
							url: data['url'] as string
						});
						break;
					case 'redirectTo':
						uni.redirectTo({
							url: data['url'] as string
						});
						break;
					case 'switchTab':
						uni.switchTab({
							url: data['url'] as string
						});
						break;
					case 'reLaunch':
						uni.reLaunch({
							url: data['url'] as string
						});
						break;
					case 'navigateBack':
						uni.navigateBack({
							delta: data['delta'] as number
						});
						break;
					default:
						uni.showModal({
							content: JSON.stringify(event.detail),
							showCancel: false
						});
						break;
				}
			},
			error(event: WebViewErrorEvent) {
				console.log(JSON.stringify(event.detail));
			},
			loading(event: WebViewLoadingEvent) {
				console.log(JSON.stringify(event.type));
			},
			loaded(event: WebViewLoadedEvent) {
				console.log(JSON.stringify(event.type));
			}
		}
	}
</script>

<style>

</style>