H5(web-view)向小程序uniApp传递参数

457 阅读1分钟

2. H5(web-view)向小程序传递参数

  1. 在小程序页面使用@message接收
<!-- 认购轨迹 -5.8.1 -->
<template>
  <web-view :src="url" @load="load" @error="error" @message="handleMessage"></web-view>
</template>
<script>
export default {
  data () {
    return {}
  },
	computed: {},
	onLoad(options) {},
  methods: {
		handleMessage(event) {
			const self = this
			console.log('从H5接收到的数据:', event.detail.data);
      self.utils.reportEvent('property_detail_mortgage_calc_click')
    },
    load (e) {
      console.log('load', e.mp.detail)
    },
    error (e) {
      console.log('error', e.mp.detail)
    }
  }
}
</script>

2. 在H5页面使用wx.miniProgram.postMessage进行数据发送

 if (wx && wx.miniProgram) {
    wx.miniProgram.postMessage({ data: ['这是从H5页面发送的消息', 'wdwdsdsf'] });
    console.log(uni.postMessage);
    console.log(111111);
  } else {
    console.error('当前环境不支持微信小程序API');
  }

3. 注意:@message在微信小程序官网描述:触发时机为 小程序后退、组件销毁、分享、复制链接
4. 官网:developers.weixin.qq.com/miniprogram…