在UniApp中使用WebView實(shí)現(xiàn)雙向通信完整代碼
為什么用WebView?
WebView是UniApp中用于嵌入網(wǎng)頁內(nèi)容的組件,允許在應(yīng)用中加載和顯示網(wǎng)頁。它適用于需要在應(yīng)用中集成外部網(wǎng)頁或HTML內(nèi)容的場景,如展示幫助文檔、加載第三方服務(wù)等。簡單來說就是:我需要在app環(huán)境做一些uniapp的api不支持的功能,如:文件上傳、音源轉(zhuǎn)碼等;
一、在vue文件中創(chuàng)建webview
插入的時(shí)候注意下,如果小伙伴用了自己src的地址發(fā)現(xiàn)頁面出不來,可以也換成百度的試一下,如果百度的出的來,那就是你地址有問題;
<template> <web-view ref="webview" id="myWebview" src="http://baidu.com" :fullscreen="false" @message="handleMessage" :webview-styles="{ process: false, }" /> <up-button @click="myClickFn">點(diǎn)擊</up-button> </template>
二、雙向通信:
2.1、uniapp→webview
- vue中發(fā)送數(shù)據(jù)到html文件
// vue中發(fā)送數(shù)據(jù)到html文件,getMsgFromApp名字自定義 myClickFn() { // #ifdef APP-PLUS //選擇到自己的webview-------h5不支持 var currentWebview = this.$parent.$scope.$getAppWebview().children()[0] currentWebview.evalJS(`getMsgFromApp(${你的數(shù)據(jù)})`) // #Endif },
- html中接收來自 uniapp 的消息
// html中接收來自 uniapp 的消息 window.getMsgFromApp = function (arg) { console.log('接收來自 uniapp 的消息,arg',arg) }
2.2、webview→uniapp
- html向uniapp 發(fā)送消息
// 向 uniapp 發(fā)送消息 function sendMessageToUniapp() { window.uni.postMessage({ data: { type: 'fromWebview', message: '這是來自 webview 的消息', }, }) }
- uniapp接收html消息
<web-view 、、、、 @message="handleMessage" /> // 接收來自 webview 的消息 handleMessage(event) { console.log('收到來自 webview 的消息:', event.detail) },
三、完整代碼
3.1UniApp:
<template> <web-view ref="webview" id="myWebview" src="http://baidu.com" :fullscreen="false" @message="handleMessage" :webview-styles="{ process: false, }" /> <up-button @click="myClickFn">點(diǎn)擊</up-button> </template> <script> export default { data() { return {} }, methods: { // 接收來自 webview 的消息 handleMessage(event) { console.log('收到來自 webview 的消息:', event.detail) }, // 發(fā)送數(shù)據(jù)到html文件 myClickFn() { // #ifdef APP-PLUS //選擇到自己的webview-------h5不支持 var currentWebview = this.$parent.$scope.$getAppWebview().children()[0] currentWebview.evalJS(`getMsgFromApp(${你的數(shù)據(jù)})`) // #Endif }, }, } </script>
3.2WebView的html:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>我的webview</title> </head> <body> <script src="xxxxxxxxxx" charset="UTF-8"></script> //引入的js文件 <script type="text/javascript" src="https://gitcode.net/dcloud/uni-app/-/raw/dev/dist/uni.webview.1.5.6.js"></script> <div id="content" class="content"> 內(nèi)容?。。。。?! </div> <script type="text/javascript"> // 接收來自 uniapp 的消息 window.getMsgFromApp = function (arg) { console.log('接收來自 uniapp 的消息') } // 向 uniapp 發(fā)送消息 function sendMessageToUniapp() { window.uni.postMessage({ data: { type: 'fromWebview', message: '這是來自 webview 的消息', }, }) } window.onload = function () { // 頁面創(chuàng)建時(shí)執(zhí)行 console.log('頁面創(chuàng)建了') } window.addEventListener('pagehide', function (event) { if (event.persisted) { // 頁面被瀏覽器緩存(如iOS Safari的后臺標(biāo)簽) console.log('頁面被緩存'); } else { // 頁面正在被銷毀 console.log('頁面被銷毀') } }); </script> </body> <style> </style> </html>
總結(jié)
到此這篇關(guān)于在UniApp中使用WebView實(shí)現(xiàn)雙向通信的文章就介紹到這了,更多相關(guān)UniApp WebView雙向通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue props對象validator自定義函數(shù)實(shí)例
今天小編就為大家分享一篇vue props對象validator自定義函數(shù)實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated
安裝vue腳手架報(bào)錯(cuò)可能具體原因比較多,可以根據(jù)報(bào)錯(cuò)信息進(jìn)行排查,本文主要介紹了npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated,感興趣的可以了解一下2023-11-11