web-view.uvue 2.9 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" @loaded="loaded" @download="download">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
5 6 7 8 9 10
    </web-view>
    <view class="uni-padding-wrap uni-common-mt">
      <view class="uni-input-v">
        <input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" />
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
11 12
        <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 已提交
13 14
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
15 16
        <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 已提交
17 18 19 20 21 22
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="nativeToWeb">原生和Web通信</button>
      </view>
    </view>
  </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23 24 25
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
26 27 28
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
29
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
30 31 32 33 34
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
35 36
        webviewContext: null as WebviewContext | null,
        loadError: false
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37 38 39
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
40 41
      // #ifdef APP
      // TODO web 实现createWebviewContext
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
42
      this.webviewContext = uni.createWebviewContext('web-view', this)
雪洛's avatar
雪洛 已提交
43
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    },
    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')");
      },
      message(event : WebViewMessageEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
62
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
63 64
      },
      error(event : WebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
65
        this.loadError = true
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
67 68
      },
      loading(event : WebViewLoadingEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
70 71
      },
      loaded(event : WebViewLoadedEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
72 73 74 75 76 77 78 79
        console.log(JSON.stringify(event));
      },
      download(event : WebViewDownloadEvent) {
        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 已提交
80 81
      },
      confirm(event : InputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
82
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83 84 85 86 87 88 89 90
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
91 92 93
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
94 95 96
  .uni-input-v {
    padding: 10rpx 0;
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
97

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98 99 100
  .uni-btn-ml {
    margin-left: 10rpx;
  }
shutao-dc's avatar
shutao-dc 已提交
101
</style>