Home > Article > WeChat Applet > Introduction to WeChat Development (11) Update the data on the previous page
There is often this demand during the development process of small programs. It is necessary to pass the current page data to the previous page, but wx.navigateBack() cannot pass the data.
The general method is to put the current page data into the local cache, and then take the previous page out of the cache.
In addition, there is another way to make clever use of the page stack.
The getCurrentPages() function is used to obtain the instance of the current page stack, which is given in the form of an array in the order of the stack. The first element is the home page, and the last element is the current page.
The key point is here, get the instance object of the previous page on the current page, and then call the method of the object to complete the data transfer.
Page A
Page({ data: { name: '' }, ... , //更新name changeData: function(name){ this.setData({ name: name }) }})
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) } }})
This way, the data can be passed to For the previous page, please note that page A must use wx.navigateTo to jump to page B, and wx.redirectTo cannot be used. This will close the previous page, causing page B to be unable to obtain the previous page Page instance.
【Related recommendations】
1. WeChat public account platform source code download
3. WeChat LaLa Takeaway 2.2.4 Decrypted Open Source Version of WeChat Rubik’s Cube Source Code
The above is the detailed content of Introduction to WeChat Development (11) Update the data on the previous page. For more information, please follow other related articles on the PHP Chinese website!