最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

web-view實(shí)現(xiàn)app應(yīng)用與網(wǎng)頁(yè)的數(shù)據(jù)交互方式

 更新時(shí)間:2025年05月15日 10:18:14   作者:愛編程的小學(xué)究  
這篇文章主要介紹了web-view實(shí)現(xiàn)app應(yīng)用與網(wǎng)頁(yè)的數(shù)據(jù)交互方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、網(wǎng)頁(yè)向app傳遞數(shù)據(jù)

1、在網(wǎng)頁(yè)項(xiàng)目的index.html頁(yè)面引入uniapp的js文件,引入成功之后就可以在事件里面使用uni.postMessage(網(wǎng)頁(yè)項(xiàng)目是用vue框架寫的)

<!-- uniapp 通信必須有的 -->
  <script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.2/index.js"></script>
  <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script>
  <script>
     document.addEventListener('UniAppJSBridgeReady', function() {  
      uni.getEnv(function(res) {  
          console.log('當(dāng)前環(huán)境:' + JSON.stringify(res));  
      });  
    });
</script>

2、向app傳遞數(shù)據(jù),可以直接寫在點(diǎn)擊事件里面

uni.postMessage({
   data: {
       action: 'scanCode'
   }
})

二、app處理接收的數(shù)據(jù)并向網(wǎng)頁(yè)傳遞數(shù)據(jù)

1、app項(xiàng)目中引入網(wǎng)頁(yè)

<web-view @message="handleMessage" ref="webview" :src="url"></web-view>

2、handleMessage接收網(wǎng)頁(yè)傳過(guò)來(lái)的數(shù)據(jù)

 handleMessage(event) {
     console.log("webView傳遞過(guò)來(lái)的消息:" + JSON.stringify(evt.detail.data))
     // #ifdef APP-PLUS
     const currentWebview = this.$parent.$scope.$getAppWebview().children()[0]
     let data = event.detail.data.length>0?event.detail.data[0]:''
     if (data&&data.action == "scanCode") {
          console.log('數(shù)據(jù)接收成功')
          // 進(jìn)行處理
     }
     // #endif
 }

3、向網(wǎng)頁(yè)中傳遞數(shù)據(jù)

開發(fā)的時(shí)候使用this.$scope一直Cannot read property ‘$getAppWebview‘ of undefined,后來(lái)查找了很多才找到,組件內(nèi)要用this.$parent.$scope

如果web-view是放在組件里面的用下面的方法去獲取web-view窗口

//如果web-view就在當(dāng)前頁(yè)面里面,可以把$parent去掉
const currentWebview = this.$parent.$scope.$getAppWebview().children()[0]
currentWebview.evalJS(`scanCode(${JSON.stringify(_data)})`);

注意:如果頁(yè)面沒(méi)有初始化完成,可以加個(gè)定時(shí)等初始化完成在調(diào)用

var currentWebview = this.$scope.$getAppWebview() //此對(duì)象相當(dāng)于html5plus里的plus.webview.currentWebview()。在uni-app里vue頁(yè)面直接使用plus.webview.currentWebview()無(wú)效
setTimeout(function() {
   wv = currentWebview.children()[0]
}, 1000); 

三、快速起服務(wù)測(cè)試

如果你想快速上手的話,可以直接建一個(gè)html頁(yè)面起本地服務(wù)

  • 1、把index.html放到文件夾下
  • 2、打開文件夾 shift+鼠標(biāo)右鍵,打開命令行
  • 3、下載http-server: npm install http-server -g
  • 4、開啟本地服務(wù)器,輸入 http-server 就可以了
  • 5、關(guān)閉本地服務(wù)器,按快捷鍵 CTRL-C,當(dāng)顯示 ^Chttp-server stopped 的時(shí)候,就是關(guān)閉成功了

用http://192.168.0.8:8080 作為url引入到web-view里面

<web-view @message="handleMessage" ref="webview" src="http://192.168.0.8:8080 "></web-view>
  • index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>網(wǎng)絡(luò)網(wǎng)頁(yè)</title>
    <style type="text/css">
      .btn {
        display: block;
        margin: 20px auto;
        padding: 5px;
        background-color: #007aff;
        border: 0;
        color: #ffffff;
        height: 40px;
        width: 200px;
      }
      .btn-red {
        background-color: #dd524d;
      }
      .post-message-section {
        visibility: hidden;
      }
    </style>
  </head>
  <body>
    <div class="post-message-section">
      <div class="btn-list">
        <button class="btn btn-red" type="button" id="postMessage">postMessage</button>
      </div>
    </div>
    <!-- uni 的 SDK -->
    <!-- uni.webview.1.5.4.js-->
    <script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js"></script>
    <script type="text/javascript">
      // 待觸發(fā) `UniAppJSBridgeReady` 事件后,即可調(diào)用 uni 的 API。
      document.addEventListener('UniAppJSBridgeReady', function() {
        uni.postMessage({
            data: {
                action: 'message'
            }
        });
        uni.getEnv(function(res) {
            console.log('當(dāng)前環(huán)境:' + JSON.stringify(res));
        });
        document.getElementById('postMessage').addEventListener('click', function() {
          uni.postMessage({
            data: {
              action: 'message'
            }
          });
        });
      });
    </script>
  </body>
</html>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

聂荣县| 万盛区| 星子县| 花莲市| 太原市| 旬阳县| 天柱县| 潼关县| 枣庄市| 河池市| 湾仔区| 新竹市| 凤庆县| 普陀区| 巴中市| 中阳县| 从化市| 外汇| 汉川市| 桐城市| 灌阳县| 曲靖市| 睢宁县| 乐安县| 乌兰县| 左贡县| 宁海县| 南平市| 常山县| 满城县| 肃南| 朔州市| 双鸭山市| 浦城县| 岫岩| 抚宁县| 武宣县| 额济纳旗| 遂宁市| 鸡东县| 石河子市|