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

青灯夜游
Release: 2021-10-12 11:24:50
forward
3259 people have browsed it

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 agetCurrentPagesAPI 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 callgetCurrentPages()duringApp.onLaunch, whenpagehas not been generated yet.

Through thegetCurrentPagesAPI, 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];
Copy after login

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 thesetDatamethodgotoPage.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.navigateBackto 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!

Related labels:
source:juejin.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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!