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

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29 30
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
31
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32 33 34 35 36
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
37 38
        webviewContext: null as WebviewContext | null,
        loadError: false
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
39 40 41
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
42 43
      // #ifdef APP
      // TODO web 实现createWebviewContext
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44
      this.webviewContext = uni.createWebviewContext('web-view', this)
雪洛's avatar
雪洛 已提交
45
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    },
    methods: {
      back() {
        this.webviewContext?.back();
      },
      forward() {
        this.webviewContext?.forward();
      },
      reload() {
        this.webviewContext?.reload();
      },
      stop() {
        this.webviewContext?.stop();
      },
      nativeToWeb() {
        this.webviewContext?.evalJS("alert('hello uni-app x')");
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
63
      message(event : UniWebViewMessageEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
65
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
67
        this.loadError = true
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
68
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
70
      loading(event : UniWebViewLoadingEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
71
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
72
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
73
      load(event : UniWebViewLoadEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
74 75
        console.log(JSON.stringify(event));
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
76
      download(event : UniWebViewDownloadEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
77 78 79 80 81
        console.log(JSON.stringify(event));
        uni.showModal({
          content: "下载链接: " + event.detail.url + "\n文件大小: " + event.detail.contentLength / 1024 + "KB",
          showCancel: false
        });
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
82
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83
      confirm(event : UniInputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
84
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
85 86 87 88 89 90 91 92
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
93 94 95
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
96
  .uni-input-v {
H
hdx 已提交
97
    padding: 5px 0;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
99

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
100
  .uni-btn-ml {
H
hdx 已提交
101
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
102
  }
shutao-dc's avatar
shutao-dc 已提交
103
</style>