search
HomeWeb Front-enduni-appuniapp hides the current page without closing it

With the continuous development of mobile development technology, cross-platform development tools are becoming more and more mature and perfect. Among them, UniApp is one of the most popular cross-platform mobile development frameworks in China. It has the characteristics of high efficiency, simplicity, and ease of use, making it the best choice for developers.

In development, we often encounter some needs that require the current page to be hidden but not closed. For example, after opening a new page, we need to hide the current page. At this time, we need to master some skills to achieve this function.

1. Routing mode of vue-router

First of all, what we need to know is that uniapp is based on the Vue framework, and vue-router is used in the Vue framework to implement routing jumps, so we can The function of hiding the current page is implemented through the routing mode of vue-router. The specific method is as follows:

  1. Set the routing mode to history in the router/index.js file
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
   mode: 'history',
   routes: [
  {
     path: '/login',
     name: 'Login',
     component: () => import('@/views/Login/Login')
  },
    // 其他路由配置...
   ]
})
  1. Use this where you need to hide the current page. $router.push implements route jump
this.$router.push({ path: '/home', query: { isHide: true }})

The query parameter is an object used to pass some data. Here we set an isHide field to mark the parameters that need to hide the current page.

  1. In the hidden page, use watch to monitor routing changes
watch: {
   '$route' () {
      if (this.$route.query.isHide) {
         this.$refs.currentView.style.display = 'none'
      }
   }
}

Here, watch is used to monitor routing changes. When isHide is true, modify the style of the current page so that Its hidden.

2. Use the v-show command in Vue

In addition to using vue-router to implement routing jumps to hide the current page, we can also use the v-show command in Vue to achieve simple implementation . The specific method is as follows:

  1. Where the current page needs to be hidden, use $emit to trigger a custom event
this.$emit('hide')
  1. Use the v-show directive in the parent component To control the current page
<template>
  <div>
    <div v-show="showCurrentPage">
      <!-- 当前页面内容 -->
    </div>

    <div v-show="showNewPage">
      <!-- 新页面内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      showCurrentPage: true, // 是否显示当前页面
      showNewPage: false // 是否显示新页面
    }
  },
  mounted () {
    // 监听自定义事件
    this.$on('hide', () => {
      this.showCurrentPage = false
    })
  }
}
</script>

Here we trigger a custom event through $emit and listen to the event in the parent component to achieve the function of hiding the current page. We can control whether to display the current page by controlling the value of the showCurrentPage variable. At the same time, we can also use this method to control whether to display a new page.

Summary

Through Vue-router’s routing mode and v-show directive, we can easily implement the function of hiding the current page. Of course, the specific implementation method still needs to be adjusted according to actual needs.

It should be noted that when using the above method to hide the current page, the memory of the current page will not be released. Therefore, if the current page is no longer needed, it is best to destroy it manually to avoid the occurrence of memory leaks. .

The above is the detailed content of uniapp hides the current page without closing it. 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 to rename UniApp download filesHow to rename UniApp download filesMar 04, 2025 pm 03:43 PM

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

How to handle file encoding with UniApp downloadHow to handle file encoding with UniApp downloadMar 04, 2025 pm 03:32 PM

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

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 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 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 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.

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools