微信小程序开发,设置小程序为可转发可分享朋友圈

微信小程序开发过程中,我们需要把小程序设置为用户可以自己转发给好友,也可以把小程序分享到朋友圈,需要按照以下步骤来设置。

1. 在page中需要定义方法

            onShareAppMessage(res) {
                // return custom share data when user share.
                console.log("分享了")
                 const promise = new Promise(resolve => {
                      setTimeout(() => {
                        resolve({
                           title: '自己定义的标题',
                          path: '/pages/index/index',
                          imageUrl: '/static/自己定义的图片路径'
                        })
                      }, 2000)
                    })
                    return {
                      title: '自己定义的标题',
                      path: '/pages/index/index',
                      imageUrl: '/static/自己定义的图片路径',
                      promise 
                    }
            },

此时微信小程序就可以转发给好友了,如果需要设置为可以分享到朋友圈,还需要再完成上一步的基础上再进行下一步。

2. 在page中定义以下方法

            onShareTimeline(res){
                console.log("分享朋友圈")
                console.log(res)
            },

此时就可以将小程序准发给好友,也可以分享到朋友圈。

除了在右上角点开设置进行分享和转发外,还可以在页面添加按钮,让用户转发,按钮如下:

                <button class="mini-btn" open-type="share">
                    <uni-icons type="redo" size="20" color="#103667"></uni-icons>分享
                </button>

,其中   <uni-icons type="redo" size="20" color="#103667"></uni-icons>  也可以不要,这就是一个转发的图标。

如下可以试用看效果

猜你喜欢

转载自blog.csdn.net/weixin_37562883/article/details/130687632