Several methods for transferring data between WeChat applet pages

青灯夜游
Release: 2021-06-22 09:19:20
forward
10786 people have browsed it

Several methods for transferring data between WeChat applet pages

In the development of WeChat applet, we often encounter the problem of data transfer or mutual influence between pages. In the actual development process, this can be achieved through the following methods.

Using global variables

Global variables actually define a global object and are introduced in each page.

When initializing the code, the applet will read an app.js file, where we can define the global variables we need.

Several methods for transferring data between WeChat applet pages

Then in the page, you can get the global application object through the getApp() method, and you can read and change the global variables:

Several methods for transferring data between WeChat applet pages

Since app.js is used for basic configuration in the project, it is not recommended to place many variables here for configuration.Generally, some persistent constants will be configured here. This method is not recommended for quantities that often need to be changed.

Use local cache

Local cache is a function provided by the WeChat applet.Can persist the data generated by the user locally, similar to NoSQL, can be performed Read and modify operations.

So how to use it to interact with data between different pages?

Suppose we save the user's information on page A.

Several methods for transferring data between WeChat applet pages

By doing this, the data will be stored locally. When needed on page B, you can directly obtain the data in the data pool and perform CRUD operations:

Several methods for transferring data between WeChat applet pages

It should be noted that in thereturn When reaching page A, the applet needs to re-read the data. At this time, you can choose to reload the data in onShow of the life cycle

Data transfer from parent to child page (template)

We usually jump between pages , redirection operation.At this time, we can choose to put some data in the url and initialize it when the new page is onLoad.

Several methods for transferring data between WeChat applet pages

In the D page, we can receive the passed parameters like this:

Several methods for transferring data between WeChat applet pages

wx.navigateTo and wx.redirectTo do not allow jumping to the page contained in the tab, only wx.switchTab can be used to jump. It should be noted that the url in wx.switchTab cannot pass parameters.

The wx.reLaunch interface newly provided by WeChat can pass in parameters.

In addition, we usually use some component templates in the page, so there will also be corresponding data transfer between parent and child.

Use the name attribute as the name of the template. Then use the is attribute here to declare the template you need to use.

Several methods for transferring data between WeChat applet pages

Then pass in the data required by the template, such as:

Several methods for transferring data between WeChat applet pages

In addition to the data passed into the template Variables can also be event method objects. For example, click events in a template can be passed to elements using the template.

Perform data operations by obtaining page objects

The essence of this method is to obtain the object prototypes of other pages,and then use the prototype method setData to perform data operations on the current object management Modify, the example is as follows:

Several methods for transferring data between WeChat applet pages

After jumping to the next page F, assuming that there are operations in F that require modification of the data in E, you can Use the following method:

1Several methods for transferring data between WeChat applet pages

This method can operate the data of the pages in the page stack,can allow the lower-level page to manage the data of the upper-level page group.

Summary

In the WeChat applet, there are the above methods but are not limited to the above methods for data transfer and interaction between pages.Can be used in combination in practical applications .For example:

  • Some constants can be managed by app.js; amounts that need to be persisted can be saved locally.

  • Data related to subordinate pages or template elements can be passed in by passing in parameters.

  • Layer-level pages can quickly modify the upper-level data by obtaining the page object in the stack.

Used together in actual applications, the data of the mini program can be better managed.

Recommendation: "Mini Program Development Tutorial"

The above is the detailed content of Several methods for transferring data between WeChat applet pages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!