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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      }
    },
    onReady() {
      this.webviewContext = uni.createWebviewContext('web-view', this)
    },
    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 已提交
59
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
60 61
      },
      error(event : WebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
62
        this.loadError = true
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
63
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
64 65
      },
      loading(event : WebViewLoadingEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
66
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
67 68
      },
      loaded(event : WebViewLoadedEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
69 70 71 72 73 74 75 76
        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 已提交
77 78
      },
      confirm(event : InputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
79
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
80 81 82 83 84 85 86 87
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
88 89 90
</script>

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

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