如何使用uniapp內(nèi)置組件webview消息傳遞詳解
前言
web-view是uni-app的一個內(nèi)置組件,可以在應(yīng)用里打開指定的網(wǎng)頁,應(yīng)用和網(wǎng)頁之間可以收發(fā)消息。
官方文檔地址:官網(wǎng)>文檔>組件>內(nèi)置組件>web-view
1.調(diào)用uni.postMessage被告知方法不存在(uni.postMessage is not a function)
官方文檔web-view有幾個相關(guān)方法,**uni.postMessage(OBJECT)**的描述是:網(wǎng)頁向應(yīng)用發(fā)送消息:
web-view是uniapp內(nèi)置組件,并有自帶的方法可以支持消息傳遞,考慮到uniapp本身可以發(fā)布成H5網(wǎng)頁,所以就直接創(chuàng)建了一個新的uniapp項目,用來做跳轉(zhuǎn)目標網(wǎng)頁。
直接在新項目index頁面測試:
<template> <button type="button" @click="sendMess">發(fā)送</button> </template> <script setup> const sendMess = (e) => { uni.postMessage({ data: { action: 'message' } }); } </script> <style lang="scss" scoped> </style>
然后在應(yīng)用里添加web-view,指向目標網(wǎng)頁:
<web-view src="目標網(wǎng)頁" @message="消息接收方法"></web-view>
在手機上測試可以正常打開網(wǎng)頁,但是點擊按鈕,調(diào)用uni.postMessage方法時報錯:
TypeError: uni.postMessage is not a function
這里先試用了uniapp其他幾個常用的內(nèi)置API,比如uni.showToast(OBJECT),來證明內(nèi)置的方法有正常加載,點擊按鈕,結(jié)果正常運行:
uni.showToast({ title: '標題', duration: 2000 });
重新閱讀文檔,發(fā)現(xiàn)在web-view加載HTML網(wǎng)頁的示例中,引入了一個uni.webview.js文件,因為這個示例是個傳統(tǒng)的HTML頁面,之前以為用的是內(nèi)置方法就忽略了:
把文件下載下來,下載地址文檔上有提供,當前最新是1.5.6:
https://gitcode.net/dcloud/uni-app/-/raw/dev/dist/uni.webview.1.5.6.js在網(wǎng)頁項目的main.js文件中對文件進行引用:
import '/static/js/uni.webview.1.5.6.js'
重新點擊按鈕,問題仍然存在,但是把uni對象打印出來,前后對比發(fā)現(xiàn)多了一個webView,展開就看到了相關(guān)方法:
所以發(fā)送方法改成:
<script setup> const sendMess = (e) => { uni.webView.postMessage({ data: { action: 'message' } }); } </script>
消息正常發(fā)送并接收。
2.H5無法接收消息
上邊用手機測試通過,但在瀏覽器中進行訪問,就接收不到消息:
官方文檔中有標示:
根據(jù)文檔需要用window.postMessage,這個方法之前使用,是在web網(wǎng)頁中嵌套iframe,父子頁面通信的時候,文檔上可以看到組件會被轉(zhuǎn)成iframe:
注意:這里只要修改應(yīng)用這邊(變成瀏覽器訪問之后就是父級頁面這里),需要以window.addEventListener監(jiān)聽消息,網(wǎng)頁端也就是子級不需要修改。
<template> <view> <web-view src="目標網(wǎng)頁" @message="listenMess"></web-view> </view> </template> <script setup> import { onBeforeUnmount } from "vue"; import { onLoad } from "@dcloudio/uni-app"; const listenMess = (e) => { console.info(e); } onLoad(() => { // #ifdef H5 window.addEventListener('message', listenMess, false); // #endif }); onBeforeUnmount(() => { // #ifdef H5 window.removeEventListener("message", listenMess); // #endif }); </script> <style lang="scss" scoped> </style>
使用方法window.addEventListener監(jiān)聽消息,添加window.removeEventListener防止監(jiān)聽重復(fù)執(zhí)行,需要注明只在H5情況下執(zhí)行,否則手機應(yīng)用會報錯:
TypeError: Cannot read property 'addEventListener' of undefined
3.根據(jù)官方文檔中的示例編寫HTML頁面
官方文檔示例是一個傳統(tǒng)的HTML頁面,寫個簡單的頁面用nginx發(fā)布出來(web-view僅支持加載網(wǎng)絡(luò)網(wǎng)頁,不支持本地html),作為目標網(wǎng)頁試一下:
<!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>測試</title> </head> <body> <button id="sendMess">發(fā)送</button> <script type="text/javascript" src="/uni.webview.1.5.6.js"></script> <script type="text/javascript"> document.getElementById('sendMess').addEventListener('click', function() { uni.postMessage({ data: { action: 'message' } }); }); </script> </body> </html>
注意:這里是uni.postMessage,和uniapp項目中調(diào)用不同。
經(jīng)測試消息可以成功傳遞。
然后文檔上還寫到uniapp項目里也能加載html網(wǎng)頁,只要把文件放在/hybrid/html中:
寫個html放進去,uni.webview.1.5.6.js放到j(luò)s路徑里:
<!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>測試</title> </head> <body> <button onclick="sendMess()">發(fā)送</button> <script src="/hybrid/html/js/uni.webview.1.5.6.js"></script> <script type="text/javascript"> function sendMess() { uni.postMessage({ data: { action: 'message' } }); } </script> </body> </html>
經(jīng)測試消息可以成功傳遞。
4.VUE項目作為網(wǎng)頁端調(diào)用uni.postMessage方法
因為之前有個vue2的項目里有相應(yīng)功能網(wǎng)頁,就拿來用做目標,然后在依賴庫里搜到了@dcloudio/uni-webview-js,就沒用上邊的js,直接安裝引入之后,實測可以正常使用:
<template> <el-button type="primary" @click="sendMess">發(fā)送</el-button> </template> <script> import uniWebview from '@dcloudio/uni-webview-js' export default { components: { uniWebview }, methods: { sendMess() { uniWebview.postMessage({ data: { action: 'message' } }); } } } </script> <style></style>
總結(jié)
到此這篇關(guān)于如何使用uniapp內(nèi)置組件webview消息傳遞的文章就介紹到這了,更多相關(guān)uniapp內(nèi)置組件webview消息傳遞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何使用fetchEventSource實現(xiàn)sse流式請求
這篇文章主要介紹了如何使用fetchEventSource實現(xiàn)sse流式請求問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08vue+elementUI中el-radio設(shè)置默認值方式
這篇文章主要介紹了vue+elementUI中el-radio設(shè)置默認值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12解決echarts圖表使用v-show控制圖表顯示不全的問題
這篇文章主要介紹了解決echarts圖表使用v-show控制圖表顯示不全的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue實現(xiàn)數(shù)據(jù)導(dǎo)出導(dǎo)入實戰(zhàn)案例
我們經(jīng)常需要在Vue搭建的后臺管理系統(tǒng)里進行數(shù)據(jù)導(dǎo)入導(dǎo)出等操作,下面這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)數(shù)據(jù)導(dǎo)出導(dǎo)入實戰(zhàn)案例的相關(guān)資料,需要的朋友可以參考下2023-01-01Vue學(xué)習(xí)之a(chǎn)xios的使用方法實例分析
這篇文章主要介紹了Vue學(xué)習(xí)之a(chǎn)xios的使用方法,結(jié)合實例形式分析了vue.js axios庫的功能及網(wǎng)絡(luò)請求相關(guān)操作技巧,需要的朋友可以參考下2020-01-01Vue?cli3.0創(chuàng)建Vue項目的簡單過程記錄
Vue CLI是一個基于Vue.js進行快速開發(fā)的完整系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于Vue?cli3.0創(chuàng)建Vue項目的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2022-08-08