web-view.uvue 4.0 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2
  <view class="uni-flex-item">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
3 4 5
    <web-view id="web-view" class="uni-flex-item" :src="src" :webview-styles="webview_styles"
      :horizontalScrollBarAccess="horizontalScrollBarAccess" :verticalScrollBarAccess="verticalScrollBarAccess"
      @message="message" @error="error" @loading="loading" @load="load" @download="download">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
6
    </web-view>
H
hdx 已提交
7
    <!-- #ifdef APP -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
8
    <view class="uni-padding-wrap uni-common-mt">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9
      <view class="uni-btn-v">
10
        <input class="uni-input" confirmType="go" placeholder="输入网址跳转" @confirm="confirm" maxlength="-1"/>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
11 12
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
13
        <button class="uni-flex-item" type="primary" @click="back">后退</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
14
        <button class="margin-left-5 uni-flex-item" type="primary" @click="forward">前进</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
15 16
      </view>
      <view class="uni-row uni-btn-v">
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
17
        <button class="uni-flex-item" type="primary" @click="reload">重新加载</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18
        <button class="margin-left-5 uni-flex-item" type="primary" @click="stop">停止加载</button>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
19 20 21 22
      </view>
      <view class="uni-btn-v">
        <button type="primary" @click="nativeToWeb">原生和Web通信</button>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
23
      <!-- #ifdef APP-ANDROID -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
24 25 26
      <view class="uni-row uni-btn-v">
        <view class="uni-row uni-flex-item align-items-center">
          <text>显示横向滚动条</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
27
          <switch :checked="true" @change="changeHorizontalScrollBarAccess"></switch>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
28 29 30
        </view>
        <view class="uni-row uni-flex-item align-items-center">
          <text>显示竖向滚动条</text>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
31
          <switch :checked="true" @change="changeVerticalScrollBarAccess"></switch>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
32
        </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
33 34
      </view>
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
35
    </view>
H
hdx 已提交
36
    <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
37
  </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
38 39 40
</template>

<script>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
41 42 43
  export default {
    data() {
      return {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
44
        src: 'https://www.dcloud.io',
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
45 46 47 48 49
        webview_styles: {
          progress: {
            color: '#FF3333'
          }
        },
shutao-dc's avatar
shutao-dc 已提交
50
        webviewContext: null as WebviewContext | null,
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
51 52 53
        loadError: false,
        horizontalScrollBarAccess: true,
        verticalScrollBarAccess: true
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
54 55 56
      }
    },
    onReady() {
雪洛's avatar
雪洛 已提交
57 58
      // #ifdef APP
      // TODO web 实现createWebviewContext
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
59
      this.webviewContext = uni.createWebviewContext('web-view', this)
雪洛's avatar
雪洛 已提交
60
      // #endif
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    },
    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 已提交
78
      message(event : UniWebViewMessageEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
79
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
80
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
81
      error(event : UniWebViewErrorEvent) {
shutao-dc's avatar
shutao-dc 已提交
82
        this.loadError = true
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
83
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
84
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
85
      loading(event : UniWebViewLoadingEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
86
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
87
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
88
      load(event : UniWebViewLoadEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
89 90
        console.log(JSON.stringify(event));
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
91
      download(event : UniWebViewDownloadEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
92 93 94 95 96
        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 已提交
97
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
98
      confirm(event : UniInputConfirmEvent) {
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
99
        console.log(JSON.stringify(event));
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
100 101 102 103 104
        let url = event.detail.value;
        if (!url.startsWith('https://') && !url.startsWith('http://')) {
          url = 'https://' + url;
        }
        this.src = url;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
105
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
106 107
      changeHorizontalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.horizontalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
108
      },
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
109 110
      changeVerticalScrollBarAccess(event : UniSwitchChangeEvent) {
        this.verticalScrollBarAccess = event.detail.value;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
111 112 113
      }
    }
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
114 115 116
</script>

<style>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
117 118
  .margin-left-5 {
    margin-left: 5px;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
119
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
120

DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
121 122
  .align-items-center {
    align-items: center;
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
123
  }
shutao-dc's avatar
shutao-dc 已提交
124
</style>