uniapp web-view組件雙向通信的問題記錄
前言
本文主要介紹在uniapp中頁面與webview組件內(nèi)頁面的雙向通信問題。
準備
uniapp項目
調用webview組件
<web-view src="/hybrid/html/index.html"></web-view>
Web項目
項目目錄
在uniapp項目根目錄下新建hybrid/html目錄,web項目文件放在hybrid/html目錄下,否則web-view無法調用項目文件。
引入官方庫
在web項目中引入官方庫uni.webview.js,可以從uniapp官方示例庫中下載,下載后放入web項目目錄下即可,本文放在js文件夾中,然后在web項目頁面中引入。
<script type="text/javascript" src="js/uni.webview.1.5.2.js"></script>
通信
uniapp傳webview
接收消息(webview)
uniapp發(fā)送消息實際上就是調用webview組件內(nèi)頁面方法實現(xiàn)通信,所以我們需要現(xiàn)在webview組件內(nèi)頁面index.html中定義一個接收消息的方法:
function webReceiveData (data) { var parseData = JSON.parse(data) document.getElementById('msg').innerText = `接收到的消息:${parseData.msg}` }
發(fā)送消息(uniapp)
要向webview中傳遞消息,首先需要獲取到當前頁面中的webview組件,然后調用webview頁面中定義的接收方法就好;如下代碼,獲取到當前頁面中的webview保存在wv中,并在獲取成功后調用sendData方法向webview發(fā)送消息,在sendData方法中實現(xiàn)了webview組件內(nèi)方法的調用。
onLoad() { let that = this; // #ifdef APP-PLUS var currentWebview = that.$scope.$getAppWebview(); let num = 0; let wv_time = setInterval(function() { num++; that.wv = currentWebview.children()[0]; if (that.wv) { // 獲取到當前頁面的webview子頁面然后下發(fā)消息 that.sendData({ type: 'init', msg: 'addd' }) clearInterval(wv_time) } }, 100); // #endif }, methods: { sendData (data) { let that = this that.wv.evalJS("webReceiveData('" + JSON.stringify(data) + "')"); }, }
webview傳uniapp
接收消息(uniapp)
在uniapp中為web-view組件添加上@message事件
<web-view src="/hybrid/html/index.html" @message="receiveData"></web-view>
處理@message事件的方法receiveData方法
receiveData (data) { console.log("收到來自webview的消息:", data.detail) }
發(fā)送消息(webview)
在webview頁面中調用uni.postMessage 方法實現(xiàn)消息發(fā)送。
function webSendData () { document.getElementById('send').innerText = `已發(fā)送的消息:啊對對對` uni.postMessage({ data: { msg: '啊對對對' } }); }
完整代碼
uniapp端
<template> <view class="content"> <web-view src="/hybrid/html/index.html" @message="receiveData"></web-view> </view> </template> <script> export default { data() { return { wv: null, } }, onLoad() { let that = this; // #ifdef APP-PLUS var currentWebview = that.$scope.$getAppWebview(); let num = 0; let wv_time = setInterval(function() { num++; that.wv = currentWebview.children()[0]; if (that.wv) { // 獲取到當前頁面的webview子頁面然后下發(fā)消息 that.sendData({ type: 'init', msg: 'addd' }) clearInterval(wv_time) } }, 100); // #endif }, methods: { sendData (data) { let that = this that.wv.evalJS("webReceiveData('" + JSON.stringify(data) + "')"); }, receiveData (data) { console.log("收到來自webview的消息:", data) }, } } </script> <style> </style>
Web端
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>通信實例</title> <style type="text/css"> .container { display: flex; flex-direction: column; } .content { text-align: center; margin: 15px 0; } #msg { font-size: 1.2rem; font-weight: bold; color: #ff7d00; } .text { text-align: center; color: #c5c8ce; } .btn-box { text-align: center; margin: 15px 0; } .btn-box button { width: 125px; height: 45px; font-size: 18px; color: white; background-color: #1989fa; border-color: #2b85e4; border-radius: 5px; } .btn-box button:active { background-color: #88c1fa; border-color: #5cadff; } </style> </head> <body> <div class="container" id="lauwen"> <div class="content" id="msg"></div> <div class="content" id="send"></div> <div class="text">?https://blog.csdn.net/Douz_lungfish</div> <div class="btn-box"> <button type="button" onclick="webSendData()">發(fā)送消息</button> </div> </div> <script type="text/javascript" src="js/uni.webview.1.5.2.js"></script> <script type="text/javascript"> function webReceiveData (data) { var parseData = JSON.parse(data) document.getElementById('msg').innerText = `接收到的消息:${parseData.msg}` } function webSendData () { document.getElementById('send').innerText = `已發(fā)送的消息:啊對對對` uni.postMessage({ data: { msg: '啊對對對' } }); } </script> </body> </html>
到此這篇關于uniapp web-view組件雙向通信的文章就介紹到這了,更多相關uniapp web-view組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
KnockoutJS 3.X API 第四章之數(shù)據(jù)控制流component綁定
這篇文章主要介紹了KnockoutJS 3.X API 第四章之數(shù)據(jù)控制流component綁定的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-10-10JavaScript基于replace+正則實現(xiàn)ES6的字符串模版功能
這篇文章主要介紹了JavaScript基于replace+正則實現(xiàn)ES6的字符串模版功能,結合實例形式分析了replace結合正則實現(xiàn)ES6字符串模板功能的具體步驟與相關操作技巧,需要的朋友可以參考下2017-04-04js用Date對象的setDate()函數(shù)對日期進行加減操作
在某個日期上加減天數(shù)來說,其實只要調用Date對象的setDate()函數(shù)就可以了,具體方法如下2014-09-09Javascript取整函數(shù)及向零取整幾種常用的方法
這篇文章主要介紹了Javascript取整函數(shù)及向零取整幾種常用的方法,每種方法都有其特點和適用場景,推薦使用Math.trunc(),因為它語義明確、代碼易讀且性能較好,需要的朋友可以參考下2025-01-01