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!
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】
Pay attention here, we are talking about the sub-page operating the data of the parent page.
There is agetCurrentPages
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.
getCurrentPages()
duringApp.onLaunch
, whenpage
has not been generated yet.Through thegetCurrentPages
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];
Sopages[pages.length - 1]
is the current page, and the parent page ispages[pages.length - 2]
, and so on, we Other ancestor pages can be obtained.
Then use thesetData
methodgotoPage.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 usewx.navigateBack
to return to the page.
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!