uniapp implements the function of sharing the current page to WeChat friends or Moments (with and without parameters)

Hello everyone! It’s Wednesday again in a hurry, and today is the day 2023年9月13日|农历七月二十九. Today’s article is mainly about WeChat applet to share the current page to Moments or WeChat friends. Let’s take a look at the specific implementation method!

uniapp implements the function of sharing to WeChat friends or Moments

With or without parameters is based on your own business needs. If you need to assign some attribute values ​​in the onLoad function, you can pathcarry parameters in it. When the user comes in through the link we shared, the parameters will be carried.

Jump without parameters

// 分享微信好友
onShareAppMessage() {
    
    
  return {
    
    
    title: '标题', //分享标题 这个可以根据业务需求去定义
    path: '当前需要进行分享页面的地址' //例如:path: 'page/home/index'
  }
},
// 分享朋友圈
onShareTimeline() {
    
    
  return {
    
    
    title: '标题', //分享标题 这个可以根据业务需求去定义
    path: '当前需要进行分享页面的地址' //例如:path: 'page/home/index'
  }
},

Jump with parameters

pathMy business requirement is to carry parameters when sharing id. When the user enters this page through the link I shared, the onLoad function needs to be triggered, and the idback-end interface is requested through the parameters I carry to render the page data.
The specific code is as follows:

// 分享微信好友
onShareAppMessage() {
    
    
  return {
    
    
    title: '标题',
    path: '/page/home/index?id='+this.id
  }
},
// 分享朋友圈
onShareTimeline() {
    
    
  return {
    
    
    title: '标题',
    path: '/page/home/index?id='+this.id
  }
},

The following is the code for the onLoad function:
I need to pass an id attribute when sharing, trigger onLoadthe function when the page is rendered, pass idthe attribute to getQuerythe function, request the backend, and achieve dynamic rendering of data.

onLoad(e) {
    
    
 console.log("e============>",e)
 if (e.hasOwnProperty('id')) {
    
    
   this.id = e.id
 }
 this.getQuery(this.id)
},

Conclusion

This is the problem and solution I encountered. I hope this blog post can solve the problem you encountered.如果有不明白的博主,或者你也遇到了类似的问题,私信我,我会一一为你进行解答,不收取任何费用,欢迎打扰哦~

Guess you like

Origin blog.csdn.net/xiaohua616/article/details/132866484