当前位置:   article > 正文

微信小程序嵌套的H5使用小程序分享(分享微信好友或朋友圈)_小程序webview分享到朋友圈

小程序webview分享到朋友圈

需求说明

嵌套在微信小程序中的H5想要使用小程序自带分享功能,分享H5的页面给微信好友或朋友圈

实现原理

H5中可使用wx.miniProgram.postMessage向小程序的webview发送消息,会触发组件的message事件,在小程序webview页面onShareAppMessage,onShareTimeline中进行使用。

代码实现

1. 在需要分享的H5页面,设置分享的相关参数。

 wxSdk.miniProgram.postMessage({//wxSdk为微信sdk
  	data: {
       shareTitle: shareTitle,//自定义
       shareUrl: shareUrl//自定义
    }
  })
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.小程序页面接收参数

<web-view :src="loadUrl" @message="handleMessage" ></web-view>
  • 1
handleMessage(evt) {
	this.shareTitle = evt.detail.shareTitle ;
	this.shareUrl =  evt.detail.shareUrl;

},
  • 1
  • 2
  • 3
  • 4
  • 5

3.分享微信好友或朋友圈

onShareAppMessage() {
	return {
		title: this.shareTitle ,
		path: this.shareUrl,
	};
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
onShareTimeline() {
	return {
		title: this.shareTitle ,
		path: this.shareUrl,
	};
},
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4.实现效果

在这里插入图片描述

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号