search
HomeWeb Front-enduni-appHow to close the middle pages in uniapp development

During the uniapp development process, we often need to close the page. But how to close only the middle pages when closing the page? This article will introduce how to implement this function in uniapp development.

Step one: Understand the page stack

In uniapp, we can use uni.navigateTo and uni.redirectTo to jump to the page, and we can also use uni.navigateBack to return to the previous page. In the process of jumping and returning to pages, the page stack plays an important role.

The page stack is an array that stores all the pages in the current page stack. Through the page stack, we can jump between pages, pass parameters, and close the page.

By default, the page stack starts from the home page (i.e. app.vue), and the top page is placed at the end of the page stack.

For example, we jump to page A through uni.navigateTo, and then jump to page B through uni.navigateTo in page A. At this time, the structure of the page stack is as follows:

[ home, A, B ]

Among them, home is the home page, and the top page is B. In page B, if we want to return to page A, we can do so through the uni.navigateBack() function.

If we want to close page B while returning to page A, how to achieve it? This requires us to operate the page stack.

Step 2: Manipulate the page stack

We can close all pages through uni.reLaunch and reopen the target page in an open page. But that's not what we need since we only want to close the middle page.

In uniapp, you can get the current page stack through the uni.getCurrentPages() method. This method returns an array that stores all pages in the current page stack. We can operate on the page stack through this array.

First of all, we can get the status of the current page stack through getCurrentPages():

let pages = getCurrentPages()

At this time, the pages array saves all the pages in the page stack. If we want to close the current page and the previous page in the page stack, we can do it like this:

let pages = getCurrentPages()
let currentPage = pages[pages.length - 1]
let prePage = pages[pages.length - 2]
currentPage.$destroy()
uni.navigateBack({
  delta: 1,
  success: function () {
    prePage.setData({
      foo: 'bar'
    })
  }
})

In this code, we first get the status of the current page stack. Then, the instances of the current page and the previous page are obtained through currentPage and prePage respectively. Then, the instance object of the current page can be destroyed through the $destroy() method.

Finally, use the uni.navigateBack() method to return to the previous page and modify the data on the previous page.

If you want to close multiple pages, you can close the pages one by one by looping through them. For example, if you want to close the current page, the previous page and the previous page, you can do it like this:

let pages = getCurrentPages()
for (let i = 0; i <p>In this way, you can realize the function of closing only the middle pages. </p><p>Summary</p><p>In uniapp development, the page stack is a very important concept. By understanding the page stack, we can realize functions such as jumping between pages, passing parameters, and closing pages. </p><p>To close the middle pages, you can use the getCurrentPages() method to obtain the status of the current page stack, close the pages one by one through loop traversal, and finally use the uni.navigateBack() method to return to the previous page. . </p><p>Hope this article can be helpful to you! </p>

The above is the detailed content of How to close the middle pages in uniapp development. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor