uniapp Sample code for implementing global sharing of WeChat mini-programs

uniapp Sample code for implementing global sharing of WeChat mini-programs

uniapp implements the WeChat applet’s global forwarding to friends/sharing to Moments function. Mainly uses the global mixin concept of Vue.js.

Below are the implementation steps and codes:

Create a global shared content file

1. Create a globally shared js file. The sample file path is: @/common/share.js , in which the global shared content is defined:

export default {
	data() {
		return {
			//Default global sharing content share: {
				title: 'Globally shared title',
				path: '/pages/home/home', // global sharing path imageUrl: '../../static/imgs/fenxiang-img.png', // global sharing image (can be local or network)
			}
		}
	},
	// Define global sharing // 1. Send to friends onShareAppMessage(res) {
        return {
			title: this.share.title,
			path: this.share.path,
			imageUrl: this.share.imageUrl,
		}
    },
	//2. Share to Moments onShareTimeline(res) {
        return {
			title: this.share.title,
			path: this.share.path,
			imageUrl: this.share.imageUrl,
		}
    },
}

Import and globally register the file

2. Import the share.js file into the main.js file of the project and use the Vue.mixin() method to mix it in globally :

// Import and mount the global sharing method import share from '@/common/share.js'
Vue.mixin(share)

Let’s take a look at the global sharing effect:

Customize page sharing content

3. If you need to customize the shared content on a specific page , you can still use the onShareAppMessage() and onShareTimeline() methods of the page to customize the shared content. The global sharing will be overwritten by the shared content defined on the page . Here is an example:

    onLoad() {},
 
    // Customize forwarding this page to friends (there is already a global sharing method, which will be overwritten here)
	onShareAppMessage(res) {
	    return {
	      title: 'Title of the page shared',
	      path: '/pages/my/my',
		  imageUrl: '../../static/imgs/mylogo.png'
	    }
	  },
	  // Share a custom page to Moments onShareTimeline(res) {
		return {
			title: 'Title of the page shared',
			path: '/pages/my/my',
			imageUrl: '../../static/imgs/mylogo.png'
		}
	},

Note: onShareAppMessage() and onShareTimeline() methods are at the same level as onLoad , methods , etc.

This concludes this article about the sample code for uniapp’s global sharing of WeChat mini-programs. For more relevant uniapp mini-program global sharing content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet uniapp realizes the left swipe to delete effect (complete code)
  • Teach you how to subcontract uniapp and mini-programs (pictures and text)
  • How to implement a global floating button in uniapp mini program development

<<:  Detailed steps for setting up and configuring nis domain services on Centos8

>>:  Basic ideas for finding errors in Web front-end development

Recommend

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

MySQL automatically inserts millions of simulated data operation code

I use Navicat as my database tool. Others are sim...

Java imports data from excel into mysql

Sometimes in our actual work, we need to import d...

How to call the browser sharing function in Vue

Preface Vue (pronounced /vjuː/, similar to view) ...

Analysis and summary of the impact of MySQL transactions on efficiency

1. Database transactions will reduce database per...

Detailed explanation of padding and abbreviations within the CSS box model

As shown above, padding values ​​are composite at...

Implementation of MySQL Multi-version Concurrency Control MVCC

Table of contents What is MVCC MVCC Implementatio...

Common rule priority issues of Nginx location

Table of contents 1. Location / Matching 2. Locat...

JavaScript+HTML to implement student information management system

Table of contents 1. Introduction 2. Rendering 3....

How to introduce Excel table plug-in into Vue

This article shares the specific code of Vue intr...

The ultimate solution for writing bash scripts with nodejs

Table of contents Preface zx library $`command` c...

How to use html2canvas to convert HTML code into images

Convert code to image using html2canvas is a very...

Solve the problem of installing Theano on Ubuntu 19

Solution: Directly in the directory where you dow...