Home > Web Front-end > uni-app > How to close the pop-up window in uniapp

How to close the pop-up window in uniapp

PHPz
Release: 2023-04-20 15:18:09
Original
3431 people have browsed it

Uniapp is a popular cross-end development framework, which allows developers to use a set of codes to build applications that can run on multiple different platforms at the same time. However, as a beginner or a developer who encounters problems during use, you may encounter the problem that the pop-up window (Popup) in uniapp cannot be closed normally. This article will introduce how to close pop-up windows in uniapp.

First of all, it is very important to understand the basic usage of pop-up windows in uniapp. Usually when we use pop-up windows, we need to first introduce the component library we need to use in the page, such as the officially provided vant component. This component library is very rich and easy to use, and it contains various pop-up window components. After introducing the component library into the page, we can use pop-up components for development. For example, the following is a simple uniapp page, which shows how to use a basic pop-up component:

<template>
  <view class="content">
    <button type="primary" @click="show = true">显示弹窗</button>
    <van-popup v-model="show" position="bottom"></van-popup>
  </view>
</template>

<script>
import { Popup } from 'vant';

export default {
  components: {
    [Popup.name]: Popup
  },
  data() {
    return {
      show: false
    }
  }
}
</script>
Copy after login

In the above example, we use the Popup component in the officially provided vant component library , introduce this component into the page, and use the v-model directive to control the display and hiding of the pop-up window. Click the "Show Pop-up Window" button to control the display of the pop-up window. At this time, a pop-up window that pops up from the bottom will be displayed on the page.

But sometimes, after we write the pop-up window code, we find that the pop-up window cannot be closed. The reason is usually the lack of an event handler to close the pop-up window. In uniapp, there are usually two ways to close pop-up windows: button closing and automatic closing.

When using a button to close, we need to add a "close" button to the pop-up window, bind a click event to the button, and trigger v-model as ## through this event #true variable to close the pop-up window. Taking the Popup component in the vant component library as an example, the code is as follows:

<template>
  <view class="content">
    <button type="primary" @click="show = true">显示弹窗</button>
    <van-popup v-model="show" position="bottom">
      <view>
        <button type="default" @click="show = false">关闭</button>
      </view>
    </van-popup>
  </view>
</template>

<script>
import { Popup } from 'vant';

export default {
  components: {
    [Popup.name]: Popup
  },
  data() {
    return {
      show: false
    }
  }
}
</script>
Copy after login
In the above code, we added a "Close" button to the pop-up window and bound a click to the button event, this event will trigger the variable

show to be false, thereby closing the pop-up window.

In addition to closing the button, we can also close the pop-up window by automatically closing it. In uniapp, we can set the time for the pop-up window to display, after which the pop-up window will automatically close. Taking the Popup component in the vant component library as an example, the code is as follows:

<template>
  <view class="content">
    <button type="primary" @click="show = true">显示弹窗</button>
    <van-popup v-model="show" position="bottom" :duration="2000"></van-popup>
  </view>
</template>

<script>
import { Popup } from 'vant';

export default {
  components: {
    [Popup.name]: Popup
  },
  data() {
    return {
      show: false
    }
  }
}
</script>
Copy after login
In the above code, we set the time for the pop-up window to automatically close to 2000 milliseconds through the

:duration attribute. That is, the pop-up window will automatically close after 2 seconds. Of course, when using automatic closing, if the user needs to close the pop-up window in advance, they can still use the button to force it to close.

The above is how to close the pop-up window in uniapp. In general, the methods of closing pop-up windows mainly include button closing and automatic closing. When writing pop-up window code, you need to pay attention to adding the corresponding event processing function. Here is a method for developing pop-up windows using the officially provided vant component library. Of course, if you are using other third-party or custom component libraries, you can also develop according to the corresponding documents. I believe that through the introduction of this article, everyone has mastered the method of closing pop-up windows in uniapp. I hope it can be helpful to all developers.

The above is the detailed content of How to close the pop-up window in uniapp. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template