Home >Web Front-end >uni-app >How to close the pop-up window in uniapp
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>
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>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.
<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>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 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!