Summary of methods for returning to the previous page with parameters in WeChat applet (three types)

不言
Release: 2018-08-10 16:17:05
Original
19542 people have browsed it

This article brings you a summary of the methods (three types) of returning to the previous page with parameters in the WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

Method 1

Put the current page data into the local cache (wx.setStorage (wx.setStorageSync), and then take the previous page out of the cache (wx.getStorage (wx.getStorageSync)) At the same time, clear the cache (wx.clearStorage (wx.clearStorageSync)) when logging out.

Method 2

1. The current page sets the data of the previous page, for example

var pages = getCurrentPages(); // 获取页面栈
var currPage = pages[pages.length - 1]; // 当前页面
var prevPage = pages[pages.length - 2]; // 上一个页面
prevPage.setData({
  mydata: {a:1, b:2} // 假数据
})
Copy after login

Of course this "mydata" must be the data on the previous page.

The data returned to the previous page is:

wx.navigateBack({
  delta: 1
})
Copy after login

2. Directly call the method name to update the data Page A

Page({
  data: {
    name: ''
  },
  ...
  ,
  //更新name
  changeData: function(name){
    this.setData({
      name: name
    })
  }
})
Copy after login

Page B, assuming there is a text box for entering a name, click the return button to update The name of page A

Page({
  //此方法用于文本框输入回调
  inputTyping: function (e) {
    //获取页面栈
    var pages = getCurrentPages();
    if(pages.length > 1){
      //上一个页面实例对象
      var prePage = pages[pages.length - 2];
      //关键在这里
      prePage.changeData(e.detail.value)
    }
  }
})
Copy after login

This way, the data can be transferred to the previous page. Please note that page A must use wx.navigateTo to jump to page B. You cannot use wx.redirectTo, which will close the previous page. A page causes page B to be unable to obtain the Page instance of the previous page.

Method 3 Set global variables in app.js, assign the value to the current page, and take it from the previous page

The method is

globalData: {
  userInfo: null,
}
Copy after login

Note: Method one and method three need to refresh the page data. The method is:

/**
* 生命周期函数--监听页面显示
*/
onShow: function () {

},
Copy after login

Recommended related articles:

Code example for caching multiple pieces of data in WeChat Mini Program

WeChat Mini Program Example: How to implement batch countdown (with code)

The above is the detailed content of Summary of methods for returning to the previous page with parameters in WeChat applet (three types). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template