Home  >  Article  >  WeChat Applet  >  How to implement parameter passing from child to parent in WeChat applet (page communication)

How to implement parameter passing from child to parent in WeChat applet (page communication)

青灯夜游
青灯夜游forward
2021-10-12 11:14:223353browse

This article will help you understand how the sub-page in the WeChat applet passes parameters to the parent page. I hope it will be helpful to you!

How to implement parameter passing from child to parent in WeChat applet (page communication)

How to pass parameters from a WeChat mini program subpage to the parent page

Sometimes when we are working on a WeChat mini program project, we will encounter The following situation: there is a list page with a filter condition. After clicking, it will jump to the page and select the condition. After selecting, return to the list page and query the data according to the filter condition. Generally, more often than not, what we see is that filtering conditions are processed through pop-up boxes. So if we want this interactive method of jumping to a page, how do we pass parameters from the child page to the parent page?

【Related learning recommendations: 小program development tutorial

Questions

  • wx.navigateBack cannot carry parameters, and the child page cannot pass parameters to the parent page.

Sub-page operates the data of the parent page

Pay attention here, we are talking about the sub-page operating the data of the parent page.

There is a getCurrentPages API in the official documentation (the official website description is as follows):

PageObject[] getCurrentPages() gets the current page stack. The first element in the array is the homepage, and the last element is the current page.

  • Do not try to modify the page stack, as this will cause routing and page status errors.
  • Do not call getCurrentPages() during App.onLaunch, when page has not been generated yet.

Through the getCurrentPages API, we can get the data of the page stack, so we can get the specified page. By modifying the parent page data on the child page, we You can realize the function of passing parameters from the child page to the parent page.

const pages = getCurrentPages();
const gotoPage = pages[pages.length - 1];

So pages[pages.length - 1] is the current page, and the parent page is pages[pages.length - 2], and so on, we Other ancestor pages can be obtained.

Then use the setData method gotoPage.setData({xxx: ''}) to modify and set the data of the parent page, so that the child page can be passed to the parent page The purpose of the parameter. Then use wx.navigateBack to return to the page.

Other solutions

  • Component
  • Pop-up box

Like ours As mentioned at the beginning of the article, if we need to pass parameters from the child page to the parent page, we can display the content in the form of a pop-up box. After selecting the corresponding data, hide the current pop-up box or component.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to implement parameter passing from child to parent in WeChat applet (page communication). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete