How does uniapp obtain shared parameters?
With the gradual increase in social traffic, the sharing function has become one of the essential functions of modern APPs. In the process of developing an APP, how to obtain sharing parameters is a crucial step in the development of sharing functions. For uniapp developers, there are two ways to obtain shared parameters, which we will introduce in detail below.
1. Obtain parameters through the sharing function of uni-app mpx (mini program)
In the development of uniapp, mpx comes with many functions necessary for the development of mini programs. It also includes the sharing function that comes with the mini program. Developers of uniapp can use this function to quickly and easily obtain shared parameters.
- Add the page that needs to be shared to the pages.json file
Add the path of the page that needs to be shared in the pages.json file, as follows:
"pages": [ { "path": "pages/index/index", "style": {} }, { "path": "pages/share/share", "style": {}, "navigationBarTitleText": "分享页面" } ],
- Set the mini program sharing configuration
In each page, you can set the sharing parameters of the mini program through the onShareAppMessage function. The sample code is as follows:
onShareAppMessage: function () { return{ title:"分享标题", path:"/pages/share/share", imageUrl:"分享图片地址", success:function(res){ console.log('分享成功') }, fail:function(res){ console.log('分享失败') } } }
Through this sample code, we can see that we can set the shared title, shared path, and shared image address, and can also perform corresponding operations after successful or failed sharing.
- Get sharing parameters in the sharing page
In the sharing page, we can get the sharing parameters through the api of uni.mp. The sample code is as follows:
import uni from 'uni.mp' export default { created() { uni.getShareInfo(function(res) { console.log(res) }) } }
In this sample code, we can see that we use the uni.mp.getShareInfo() API to obtain the sharing parameters.
2. Obtain shared parameters through the wx object of the mini program
In uniapp, you can directly use the API of the mini program to obtain the shared parameters. Similarly, we can also use the wx object of the mini program to obtain the shared parameters. Here is a detailed introduction on how to use it.
- Add the path of the page that needs to be shared to the pages.json file
Similar to the above method, you need to add the path of the page that needs to be shared to the pages.json file , for example:
"pages": [ { "path": "pages/index/index", "style": {} }, { "path": "pages/share/share", "style": {}, "navigationBarTitleText": "分享页面" } ],
- Set the sharing parameters into the onShareAppMessage() function
In the page that needs to be shared, use the onShareAppMessage() function to set the sharing parameters, as follows Display:
onShareAppMessage: function () { return { title: '分享标题', path: '/pages/share/share', imageUrl: '分享图片地址' } }
In this example, we can also set the shared title, sharing path, and shared image address.
- Get sharing parameters through wx.getShareInfo()
In the shared page, use wx.getShareInfo() to obtain the encrypted data during sharing, and then use The decryption function of the applet decrypts the data, as shown below:
// 获取分享参数 onLoad: function(options) { var that = this; //获取分享加密数据 wx.getShareInfo({ shareTicket: options.shareTicket, success: function(res) { //解密数据 wx.request({ url: '', data: { "encryptedData": res.encryptedData, "iv": res.iv, "sessionKey": that.data.sessionKey }, success: function(res) { console.log(res.data); } }) } }) }
In this sample code, we use wx.getShareInfo() to obtain the shared encrypted data, and then use the formula to decrypt and obtain the sharing parameters.
Summary
Through the above two methods, we can easily obtain the sharing parameters and realize our own sharing function. During the specific implementation process, we need to pay attention to some details to ensure the normal use of the sharing function. At the same time, uniapp also provides very comprehensive API documentation, which can be viewed and used at any time during the development process.
The above is the detailed content of How does uniapp obtain shared parameters?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

Lazy loading defers non-critical resources to improve site performance, reducing load times and data usage. Key practices include prioritizing critical content and using efficient APIs.

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

The article discusses strategies to optimize UniApp loading speed, focusing on minimizing bundle size, optimizing media, caching, code splitting, using CDNs, and reducing network requests.

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.
