uni-app打包微信小程序通过web-view嵌入h5页面,h5也是uni-app开发的,怎么在小程序端通过web-view在h5的window上挂载全局方法
3***@qq.com
- 发布:2025-04-11 14:17
- 更新:2025-04-11 14:20
- 阅读:1263
3***@qq.com (作者)
我在小程序上定义了一些业务的function,然后我想通过h5调用小程序的这些function,然后获取function调用后返回的结果
2025-04-11 14:28
DCloud_UNI_JBB
回复 3***@qq.com: 有示例代码吗
2025-04-11 14:35
3***@qq.com (作者)
回复 DCloud_UNI_JBB: <template>
<web-view ref="webviewRef" :src="pageUrl" @message="getMessage" @load="loadWebView"></web-view>
</template>
<script setup>
import { ref, computed, onMounted, nextTick } from 'vue'
import { onLoad, onShareAppMessage } from '@dcloudio/uni-app'
import { useAccountStore } from '@/store'
const store = useAccountStore()
const pagePath = ref(null)
const webviewRef = ref(null)
onLoad((val) => {
uni.hideHomeButton()
pagePath.value = val.path
})
const pageUrl = computed(() => {
const { authorization, userinfo } = store
const unitId = userinfo?.unitId
const tokenName = authorization?.tokenName
const tokenValue = authorization?.tokenValue
return
${pagePath.value}?tokenName=${tokenName}&tokenValue=${tokenValue}&unitId=${unitId}})
// 接收来自 H5 页面的消息
function getMessage (e) {
const data = JSON.parse(e.detail.data)
console.log('获取参数', data.callBack)
}
onMounted(() => {
let pages = getCurrentPages()
let page = pages[pages.length - 1]
console.log('弄好啊currentWebview', page)
// webviewRef.window.ethereum = window.ethereum;
})
function loadWebView (e) {
console.log('加载完成', e)
}
</script>
2025-04-11 14:37
3***@qq.com (作者)
web-view写一个function,怎么挂载到h5的window上
2025-04-11 14:38
DCloud_UNI_JBB
回复 3***@qq.com: 试试postMessage
2025-04-11 14:43
3***@qq.com (作者)
回复 DCloud_UNI_JBB: postMessage只能发消息,不能拿到主应用的方法
2025-04-11 15:20