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

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 Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

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.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software