search
HomeWeChat AppletMini Program DevelopmentHow to implement parameter passing from child to parent in WeChat applet (page communication)

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:掘金社区. If there is any infringement, please contact admin@php.cn delete

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor