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:
- 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') }, // 其他路由配置... ] })
- 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.
- 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:
- Where the current page needs to be hidden, use $emit to trigger a custom event
this.$emit('hide')
- 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!

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

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

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

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

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

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver CS6
Visual web development tools
