Home WeChat Applet Mini Program Development Summary of methods for returning to the previous page with parameters in WeChat applet (three types)

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

Aug 10, 2018 pm 04:17 PM
Page parameter passing

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)