Example of how to implement value transfer between WeChat mini program pages

Example of how to implement value transfer between WeChat mini program pages

Passing values ​​between mini program pages

Good evening everyone. I say good evening because I wrote this at night. I say this because the beginning of this sentence is not so sudden. As for the value passing between pages of the mini program, during the time I have been using it, I have very subjectively divided them into wx.navigateTo and non-wx.navigateTo, because wx.navigateTo has an event parameter event. I jump from the current page to the next page. If I need to return, I use wx.navigateTo. The function of this event is to receive the parameters returned by the next page. Like this:

index.js

wx.navigateTo({
  url: url,
  events: {
    // Add a listener for the specified event to get the data sent from the opened page to the current page acceptDataFromOpenedPage: (data) => { // This method is named casually, there are not many requirements, but the first parameter of the opened page emit should be the method name console.log('sour bean buns thrown from next door', data)
    },
  },
  success: function (res) {
    // Send data to the opened page through eventChannel // res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
  }
})

gebi.js

// Confirm the selection confirm() {
  const eventChannel = this.getOpenerEventChannel() // This should be WeChat's own method, just use it, it's OK eventChannel.emit('acceptDataFromOpenedPage', data) // The name of the method that receives parameters in the previous page event wx.navigateBack() // Return to the previous page }

These two operations complete the parent-child component value transfer similar to vue2.x, and the emit is exactly the same.

How to pass the value to the next page? When store and storage are not used, you can use the method with parameters after the Url or the success callback of wx.navigateTo above. Although I have not used the success callback, after taking a look at it, I feel that it is very similar to the way the main thread and child thread receive data when I use webworker to pass values ​​to the child thread and the child thread passes values ​​to the main thread. To put it simply, I think it is 'monitoring' (addeventlistener) (0o-_^o)
I want to mention here that the URL path is preceded by pages with a / when jumping, that's it.

wx.navigateTo({
  url: '/pages/index/index'
})

URL value transfer of mini program

The URL value passing of the mini program is the same as our ordinary routing parameters, both of which are followed by a question mark (?) and an ampersand (&), but it is divided into basic type data value passing and reference type data value passing. The common ones are as follows:

wx.navigateTo({
  url: '/pages/index/index?page=/pages/home/home&id=0077FF'
})

Yes, you read it right, you can pass a value like '/pages/home/home'. Other special characters should be converted, but I haven't tried it.

URL transfer object of applet

If you pass an object or array:

Transmission: Convert to string first, then encode.

Receive: Decode first, then convert the object.

data = {
	name: '包子',
  type: 'Beef Vermicelli'
}
wx.navigateTo({
  url: `/pages/index/index?page=/pages/home/home&params=${encodeURIComponent(JSON.stringify(data))}`
})
onLoad (options) {
	const {page} = options;
	const params = JSON.parse(decodeURIComponent(options.params))
}

Well, just spread it like this, there is nothing wrong with it.

Mention store

I use mobx in this project. The array data obtained in mobx becomes very strange. The solution is unknown. There is a toJS() method in mobx, just use it.

import { toJS } from 'mobx-miniprogram';
let value = toJS(xxxx)

Spin Jump

Regarding jumps, the official website of the mini program explains it very clearly, so I will make a table here.

wx.navigateTo Jump to a page and return. The page stack can be up to 10 layers. The internal method of event can get the data returned by the next page.
wx.navigateBack Return to the previous page or multiple pages, getCurrentPages (I have never used it, I just use wx.navigateBack()) to get the current page stack
wx.reLaunch Close all pages and jump directly to a certain page
wx.redirectTo Close the current page and jump directly to a certain page

wx.switchTab. -0-0----> Jump to the tabBar page and close all other non-tabBar pages (original words from the official website)

Regarding EventChannel, it is the value passing between pages mentioned above. I only used emit. I have not used the rest of off, on, once, so it is difficult to explain because it is difficult to understand without the scenario. I guess I can write something by using the publish-subscribe model.

Do you think the two tables are very clearly divided? Hehehe. In fact, I didn’t use the markdown table well, and it feels more appropriate to separate them.

Summarize

This is the end of this article about transferring values ​​between WeChat Mini Program pages. For more relevant content about transferring values ​​between WeChat Mini Program pages, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet page value transfer detailed explanation
  • Analysis of the method of implementing page jump value transfer and value acquisition in WeChat applet
  • WeChat applet custom component value transfer page and component data transfer operation example
  • Detailed explanation of the example of passing values ​​from child page to parent page in WeChat applet
  • How to transfer values ​​when redirecting to WeChat Mini Program page
  • Analysis of the method of transferring values ​​between two pages in WeChat applet
  • WeChat applet page value transfer and page value operation example analysis
  • WeChat applet page jump value implementation code
  • How to implement page jump and value transfer in WeChat applet
  • WeChat applet page value transfer example analysis

<<:  Detailed example of IOS database upgrade data migration

>>:  Detailed explanation of using Nginx reverse proxy to solve cross-domain problems

Recommend

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

WeChat applet implements form verification

WeChat applet form validation, for your reference...

8 essential JavaScript code snippets for your project

Table of contents 1. Get the file extension 2. Co...

How to set the default value of a MySQL field

Table of contents Preface: 1. Default value relat...

The most detailed method to install docker on CentOS 8

Install Docker on CentOS 8 Official documentation...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

How to redraw Button as a circle in XAML

When using XAML layout, sometimes in order to make...

Implementation of Nginx load balancing/SSL configuration

What is load balancing? When a domain name points...

MySql inserts data successfully but reports [Err] 1055 error solution

1. Question: I have been doing insert operations ...

The big role of HTML meta

There are two meta attributes: name and http-equiv...

How to use Node.js to determine whether a png image has transparent pixels

background PNG images take up more storage space ...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

0. Introduction August 18, 2016 Today, I noticed ...

Discussion on more reasonable creation rules for MySQL string indexes

Preface Regarding the use of MySQL indexes, we ha...

MySQL Index Detailed Explanation

Table of contents 1. Index Basics 1.1 Introductio...