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
How do I handle local storage in uni-app?How do I handle local storage in uni-app?Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How do I manage state in uni-app using Vuex or Pinia?How do I manage state in uni-app using Vuex or Pinia?Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I make API requests and handle data in uni-app?How do I make API requests and handle data in uni-app?Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I use uni-app's geolocation APIs?How do I use uni-app's geolocation APIs?Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I use uni-app's social sharing APIs?How do I use uni-app's social sharing APIs?Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration?How do I use uni-app's easycom feature for automatic component registration?Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

How do I use preprocessors (Sass, Less) with uni-app?How do I use preprocessors (Sass, Less) with uni-app?Mar 18, 2025 pm 12:20 PM

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

How do I use uni-app's uni.request API for making HTTP requests?How do I use uni-app's uni.request API for making HTTP requests?Mar 11, 2025 pm 07:13 PM

This article details uni.request API in uni-app for making HTTP requests. It covers basic usage, advanced options (methods, headers, data types), robust error handling techniques (fail callbacks, status code checks), and integration with authenticat

See all articles

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

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.