uniapp?vue3中使用webview在微信小程序?qū)崿F(xiàn)雙向通訊功能
直接上圖,注意事項是這里
官網(wǎng)鏈接: https://uniapp.dcloud.net.cn/component/web-view.html
傳遞方法的話好像只能通過url來傳,其它方式不支持,,,我這個參數(shù)沒做處理,用的話記得把參數(shù)做一下處理
也就是說傳遞數(shù)據(jù)之后需要uni.redirectTo({ url: '/pages/testFabric/index' })
特定時機(jī),當(dāng)然用其他的也行,比如uni.navigateBack()
,但是在我這不好使。這樣vue中 @message="handlerMessage"
才會觸發(fā)
下面是代碼
vue3 ts代碼 src就是項目啟動的網(wǎng)絡(luò)地址,他必須要用網(wǎng)絡(luò)地址, scr后面拼接參數(shù)記得做處理,轉(zhuǎn)成json
<template> <view> <web-view src="http://xxxx:5173/src/static/fabric.html?iii=0" @message="handlerMessage" ref="webview" ></web-view> <view class="button">evalJs(改變webview背景顏色)</view> </view> </template> <script lang="ts" setup> import { ref } from 'vue' const webview = ref(null) const handlerMessage = (e:any) => { console.log('快來---------') console.log('webview的', e) console.log('webview的webview', webview.value) } </script> <style lang="scss" scoped> .test { position: relative; } .button { position: absolute; top: 1200rpx; left: 0; } </style>
注意這里的script標(biāo)簽,需要引入
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <button class="btn" id="btn">點擊</button> 測試fabric 來一個window </body> <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js" ></script> <script> const button = document.getElementsByClassName('btn') const button2 = document.querySelector('.btn') button2.addEventListener('click', () => { console.log('h5 click') uni.postMessage({ data: { action: 'autoLogin' } }) // uni.navigateBack() // 可以跳轉(zhuǎn) uni.redirectTo({ url: '/pages/testFabric/index' }) uni.getEnv(function (res) { console.log('當(dāng)前環(huán)境:' + JSON.stringify(res)) }) }) </script> </html>
需要說明一下的是你在html中寫console.log的時候,在微信小程序不觸發(fā)是正常的,不要想著用console.log去調(diào)試了,alert是可以用的
因為小程序內(nèi)嵌的是一個網(wǎng)頁,網(wǎng)頁是支持放大縮小的,但是沒有辦法規(guī)定死不讓他放大縮小,只能在mate
標(biāo)簽上加點限制了
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no">
總結(jié)
到此這篇關(guān)于uniapp vue3中使用webview在微信小程序?qū)崿F(xiàn)雙向通訊功能的文章就介紹到這了,更多相關(guān)uniapp vue3 webview小程序雙向通訊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vuex如何在非組件中調(diào)用mutations方法
這篇文章主要介紹了vuex如何在非組件中調(diào)用mutations方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03vue 中使用 vxe-table 制作可編輯表格的使用過程
這篇文章主要介紹了vue 中使用 vxe-table 制作可編輯表格的使用過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08vue中設(shè)置echarts寬度自適應(yīng)的代碼步驟
這篇文章主要介紹了vue中設(shè)置echarts寬度自適應(yīng)的問題及解決方案,常常需要做到echarts圖表的自適應(yīng),一般是根據(jù)頁面的寬度做對應(yīng)的適應(yīng),本文記錄一下設(shè)置echarts圖表的自適應(yīng)的步驟,需要的朋友可以參考下2022-09-09