uniapp web-view 传递当前位置信息给网页
<template>
<view>
<web-view :src='url' ref='webView'></web-view>
</view>
</template>
<script>
export default {
data() {
return {
url: 'http://m.amap.com/search/view/keywords=厕所'
}
},
mounted() {
// 获取当前位置信息
uni.getLocation({
success: res => {
const position = {
latitude: res.latitude,
longitude: res.longitude
}
// 向网页发送位置信息
this.$refs.webView.postMessage(position)
}
})
// 监听网页发送的消息
window.addEventListener('message', event => {
console.log('收到网页消息:', event.data)
})
}
}
</script>
<p>在上面的代码中,通过 uni.getLocation 方法获取当前位置信息,并通过 postMessage 方法向网页发送位置信息。网页通过监听 message 事件来接收消息,并在控制台打印出来。</p>
原文地址: https://www.cveoy.top/t/topic/lc0Y 著作权归作者所有。请勿转载和采集!