Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

mpvue中web-view怎么使用bindmessage?

如题,需求是h5页面传参给小程序接收,代码如下:

h5:

<template>
  <div>
    <button @click="postMessage">click</button>
  </div>
</template>

<script>
import wx from 'weixin-js-sdk'

export default {
  data () {
    return {
    }
  },

  methods: {
    postMessage () {
      const params = {
        wboid: 'data',
        wid: 'http://localhost:8080'
      }
      const data = JSON.stringify(params)
      wx.miniProgram.postMessage({ data })
    }
  }
}
</script>

小程序:

<template>
  <div>
    <web-view
      :src="url"
      @message="bindmessage"
    ></web-view>
  </div>
</template>

<script>
export default {
  data () {
    return {
      url: 'http://localhost:8081'
    }
  },

  methods: {
    bindmessage (e) {
      console.log(e)
    }
  }
}
</script>

h5的服务是localhost:8081,小程序是8080。
我在点击h5按钮的时候,小程序点击进入web-view的page直接就跳转到h5页面了,也没有打印任何信息,求解。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
网页向小程序 postMessage 时,会在特定时机(小程序后退、组件销毁、分享)触发并收到消息。e.detail = { data },data是多次 postMessage 的参数组成的数组

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...