Wechat applet returns to the previous page to pass parameters and refresh process analysis

Time:2021-6-22

This article mainly introduces the wechat applet to return to the previous page to pass parameters and refresh the process of parsing. The sample code is introduced in great detail in this article, which has a certain reference learning value for everyone’s study or work. Friends in need can refer to it

problem

The onload (options) method of wechat applet is only loaded once in the whole life cycle, that is, when you enter the next page and return, it will not be triggered again, so it is not feasible for you to return URL to pass parameters.

demand

Now there is such a demand: a product payment page, click the coupon volume to enter the coupon list page, select the coupon and return to the payment page with the data.

method

1、 The method used is the page stack of small programs. If you are interested, you can print the contents of pages, which is the track array of your page Jump. The last item is the current page, and the penultimate is the previous page. It’s easy to understand, because it stopped on the current page, and it must be the last one

//Get page stack
let pages = getCurrentPages();
//Get the required page
let currPage = pages[pages.length - 1];// Current page
let prevPage = pages[pages.length -2];// previous page

2、 On the payment page, put the data that needs to be refreshed again into Onshow () method, and those that do not need to be refreshed into onload () method

thinking

1. Our goal is to render the coupon data to the payment page, so when you click the coupon on the coupon page, you can get the previous page (that is, the payment page) through the page stack, and add this data to the data of the payment page dynamically

//Get page stack
let pages = getCurrentPages();
//Get the required page
let prevPage = pages[pages.length -2];// previous page
prevPage.setData({
  yourD ata:yourData , // the data you need to pass
});

2. Put the data to be refreshed into the Onshow () method

//This shows how to get the options in the onload method of the current page in the Onshow method
onShow:function(){
  let pages = getCurrentPages();
  let currPage = pages[pages.length - 1];
  //The options of the current page, what do you mean, is that you may need to refresh a function, but its parameters are just the passed parameters
  console.log(currPage.options); 
}

After you click on the coupon page, because your data has been saved to the payment page, you can directly get the data you need in the data after you return to the payment page and perform the operations you need.

OK, that’s it.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.