以函数值组件里面使用为例


	import Taro, {
	 useShareAppMessage,
	 useShareTimeline,
	} from '@tarojs/taro'


    //首先定义权限
	useEffect(() => {
	    //方法一 :Taro 组件调用,需要引用Taro
	    Taro.showShareMenu({
	      withShareTicket: true,
	      showShareItems: ['shareAppMessage', 'shareTimeline'],
	      success(res) {
	        console.log(res)
	      },
	      fail(e) {
	        console.log(e)
	      },
	    })
	    
	    //方法二 : 小程序直接调用
	    wx.showShareMenu({
	      withShareTicket: true,
	      menus: ['shareAppMessage', 'shareTimeline'],
	    })
    }, [])

         //只有定义了此事件处理函数,右上角菜单才会显示“转发”按钮
	    useShareAppMessage((res) => {
		    console.log(res, '信息')
		    if (res.from === 'button') {
		      // 来自页面内转发按钮
		      console.log(res.target)
		    }
		    return {
		      title: '自定义转发标题111',
		      path: '路径',
		    }
	  })

       //只有定义了此事件处理函数,右上角菜单才会显示“分享到朋友圈”按钮
	  useShareTimeline(() => {
		    console.log('onShareTimeline')
		    return {
		      title: '自定义标题',
		      query: '路径',
		    }
	  })
微信官方参考文档 :
  1. https://developers.weixin.qq.com/miniprogram/dev/api/share/wx.showShareMenu.html
  2. https://developers.weixin.qq.com/miniprogram/dev/reference/api/Page.html#onShareAppMessage-Object-object
Taro官方参考文档 :
  1. https://docs.taro.zone/docs/apis/share/showShareMenu
  2. https://docs.taro.zone/docs/hooks#useshareappmessage
文档链接1 — showShareMenu相关
文档链接2 — useShareAppMessage与useShareTimeline相关)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐