Summary of 4 solutions for returning values ​​on WeChat Mini Program pages

Summary of 4 solutions for returning values ​​on WeChat Mini Program pages

Usage scenarios

The applet jumps from page A to page B, selects a value on page B, and then returns to page A, using the value selected on page B on page A. For example: on the purchase order page, jump to the address list, and after selecting the address, return to the order page. The delivery address on the order page needs to be updated synchronously.

Solution

The common and easier solution is to use the applet's global storage globalData, local cache storage, get the applet's page stack, call the setData method of the previous Page, and use the events property of wx.navigateTo to listen to the data sent by the opened page to the current page. Here is a brief comparison of the advantages and disadvantages of the four methods:

1. Use globalData to implement

//page A
const app = getApp() //Get the App.js instance onShow() { //Life cycle function--listen to page display if (app.globalData.backData) {
    this.setData({ //Render the updated value of page B to the page backData: app.globalData.backData
    },()=>{
     	delete app.globalData.backData //Delete data to avoid repeated rendering of onShow})
  }
}
//page B
const app = getApp() //Get the App.js instance changeBackData(){
   app.globalData.backData = 'I was modified'
   wx.navigateBack()
}

2. Use local cache Storage to implement

//page A
  onShow: function () {
    let backData = wx.getStorageSync('backData')
    if(backData){
       this.setData({
     		 backData
    	},()=>{
     		 wx.removeStorageSync('backData')
    	})
    }
  },
 //page B
 changeBackData(){
    wx.setStorageSync('backData', 'I was modified')
    wx.navigateBack()
 },

3. Use the Page stack of the applet to implement

The page stack of the mini program is more convenient than the other two methods and renders faster. There is no need to wait until you return to page A to render the data. The value on page A will be directly updated on page B. When you return to page A, the value has been updated. The principle of globalData and Storage is to modify the value on page B, then return to page A, trigger the onShow lifecycle function, and update the page rendering.

//page B
changeBackData(){
    const pages = getCurrentPages();
    const beforePage = pages[pages.length - 2]
    beforePage.setData({ // Will directly update the data of page A, and page A does not need other operations backData: "I have been modified"
    })
}

4. Implementation of events using wx.navigateTo API

The implementation principle of wx.navigateTo events is implemented by using the publish-subscribe model of the design pattern. Students who are interested can implement a simple one by themselves to achieve the same effect.

//page A
 goPageB() {
    wx.navigateTo({
      url: 'B',
      events: {
        getBackData: res => { //Add listener event in events this.setData({
            backData: res.backData
          })
        },
      },
    })
  },
//page B	
changeBackData(){
    const eventChannel = this.getOpenerEventChannel()
       eventChannel.emit('getBackData', {  
     	 backData: 'I have been modified'
    });
     wx.navigateBack()
 }

Summarize

Methods 1 and 2 are slightly slower than the latter two in terms of page rendering effect. Methods 3 and 4 have triggered the update before page B falls back to page A, while methods 1 and 2 trigger the update on page A after returning to page A. And for methods 1 and 2, you need to consider that after page A is updated, the data in globalData and Storage must be deleted to avoid repeated triggering of setData in the onShow method to update the page. Therefore, I personally recommend that you use the following methods 3 and 4.

This concludes this article about 4 solutions for returning and passing values ​​on WeChat Mini Program pages. For more relevant content on returning and passing values ​​on WeChat Mini Program pages, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of several methods of passing and obtaining values ​​in WeChat applet
  • WeChat applet page value transfer detailed explanation
  • WeChat applet custom component encapsulation and parent-child component value transfer method
  • WeChat applet data packaging, parameter value transfer and other experience sharing
  • 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 how to pass and get values ​​in WeChat applet
  • 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
  • WeChat Mini Program: Detailed Explanation of Data Storage, Value Transfer and Value Retrieval

<<:  How to use Docker buildx to build multi-platform images and push them to private repositories

>>:  Method for implementing performance testing of MySQL database through sysbench tool

Recommend

More Ways to Use Angle Brackets in Bash

Preface In this article, we will continue to expl...

Reflection and Proxy in Front-end JavaScript

Table of contents 1. What is reflection? 2. Refle...

Analysis of the principle of Vue nextTick

Table of contents Event Loop miscroTask (microtas...

SQL Practice Exercise: Online Mall Database Product Category Data Operation

Online shopping mall database-product category da...

Detailed explanation of generic cases in TypeScript

Definition of Generics // Requirement 1: Generics...

How to decrypt Linux version information

Displaying and interpreting information about you...

The difference between name and value in input tag

type is the control used for input and output in t...

Docker renames the image name and TAG operation

When using docker images, images with both REPOSI...

Detailed explanation of HTML form elements (Part 1)

HTML forms are used to collect different types of...

Ant Design Blazor component library's routing reuse multi-tab function

Recently, there has been a growing demand for imp...

MySQL json format data query operation

The default table name is base_data and the json ...

Implementing a web player with JavaScript

Today I will share with you how to write a player...

Detailed explanation of whereis example to find a specific program in Linux

Linux finds a specific program where is The where...