VUE2 event-driven pop-up window implementation example
This article mainly introduces the example of event-driven pop-up window implemented by VUE2. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
A few days ago I wanted to know how to write a pop-up window component in vue
There are two possible writing methods:
1. Status management if the pop-up window The component is placed in the root component, and vuex is used to manage the show and hide of the component. Place it in the component and control it by adding v-show or v-if. It can be combined with slot to define pop-up windows with different needs
2. Event management registers a global event to open the pop-up window and pass it The text to be displayed and related logic control can be combined with promises to achieve asynchronous
I think event-driven is better for pop-up windows like confirme and propmt. The best thing is to use promise callbacks.
So I wrote one because my hands were itchy. Below is the code.
propmt.js
import Vue from 'vue' import promptComponent from './prompt.vue' // 引入弹窗的vue文件 const promptConstructor = Vue.extend(promptComponent); // 注册组件 let instance = new promptConstructor().$mount(''); // 获得组件的实例 document.body.appendChild(instance.$el); // 将组件的element插入到body中 const Alert = (text,okText)=>{ if(instance.show === true) { //防止多次触发 return; } // 为弹窗数据赋值 instance.show = true; instance.isAlert = true; instance.okText = okText||'确定'; instance.message = text; //返回一个promise对象,并为按钮添加事件监听 return new Promise(function(resolve,reject) { instance.$refs.okBtn.addEventListener('click',function() { instance.show = false; resolve(true); }) }) }; const Confirm = (text,okText,cancelText)=>{ if(instance.show === true) { return; } instance.show = true; instance.okText = okText||'确定'; instance.cancelText = cancelText||'取消'; instance.message = text; return new Promise(function(resolve,reject) { instance.$refs.cancelBtn.addEventListener('click',function() { instance.show = false; resolve(false); }); instance.$refs.okBtn.addEventListener('click',function() { instance.show = false; resolve(true); }) }) }; const Prompt = (text,okText,inputType, defaultValue)=>{ if(instance.show === true) { return; } instance.show = true; instance.isPrompt = true; instance.okText = okText||'确定'; instance.message = text; instance.inputType = inputType || 'text'; instance.inputValue = defaultValue || ''; return new Promise(function(resolve,reject) { instance.$refs.okBtn.addEventListener('click',function() { instance.show = false; resolve(instance.inputValue); }) }) }; export {Alert,Confirm,Prompt}
prompt.vue
<style lang="less" scoped> .confirm-enter-active { transition: all .2s; } .confirm-leave-active { transition: opacity .2s; } .confirm-leave-to { opacity: 0; } .confirm-enter { opacity: 0; } .confirm { position: relative; font-family: PingFangSC-Regular; font-size: 17px; -webkit-user-select: none; user-select: none; // 遮罩层样式 .masker { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, .4); -webkit-transition: opacity .1s linear; transition: opacity .1s linear; z-index: 100; } // 入库数据错误样式 .box { position: absolute; top: 50%; left: 50%; width: 72%; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; border-radius: 12px; background-color: #fff; .message { height: 97px; line-height: 24px; font-family: PingFangSC-Regular; font-size: 17px; vertical-align: middle; color: #999; letter-spacing: -0.41px; p { margin: 20px auto 0 auto; vertical-align: middle; } &::after { content: ''; height: 100%; } } .prompt { margin: 20px 0; width: 100%; p { margin: 5px auto; font-size: 17px; line-height: 24px; } input { margin: 5px auto; border: 1px solid #333; border-radius: 6px; width: 100px; height: 30px; font-size: 14px; line-height: 20px; text-align: center; } } .button-group { a { width: calc(50% - 0.5px); text-align: center; font-size: 17px; line-height: 43px; color: blue; } .max-width { width: 100% !important;; } } } } </style> <template> <transition name="confirm"> <p class="confirm" v-show="show"> <p class="masker" @touchmove.prevent> <p class="box"> <p class="message" v-if="!isPrompt"> <p>{{message}}</p> </p> <p class="prompt" v-if="isPrompt"> <p>{{message}}</p> <input type="text" v-model="inputValue" v-if="inputType === 'text'" ref="inputEl"> <input type="tel" v-model.number="inputValue" @keydown="enterCheck" v-if="inputType === 'tel'" ref="inputEl"> </p> <p class="button-group clearfix bd-top"> <a class="bd-right fl" ref="cancelBtn" v-show="!isAlert && !isPrompt">{{cancelText}}</a> <a class="fr" ref="okBtn" :class="{'max-width': isAlert || isPrompt}">{{okText}}</a> </p> </p> </p> </p> </transition> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' export default{ data() { return { show: false, message: '请输入提示语', okText: '确定', cancelText: '取消', isAlert: false, isPrompt: false, inputValue: '', inputType: '' } }, methods: { // 金额输入框校验 enterCheck(event) { // 只允许输入数字,删除键,11位数字 if (event.keyCode === 46 || event.keyCode === 8) { return; } if (event.keyCode < 47 || event.keyCode > 58 || event.keyCode === 190) { event.returnValue = false; } }, }, watch: { show(){ if (this.show) { this.$nextTick(() => { console.log(this.$refs.inputEl); console.log(this.inputType); this.$refs.inputEl.focus(); }); } } } } </script>
main.js
import {Alert,Prompt,Confirm} from '../lib/components/prompt/prompt.js' Vue.prototype.Alert = function(text,okText) { return Alert(text,okText) }; Vue.prototype.Confirm = function(text,okText,cancelText) { return Confirm(text,okText,cancelText) }; Vue.prototype.Prompt = function(text,okText,inputType, defaultValue) { return Prompt(text,okText,inputType, defaultValue) }; component.vue: inputName() { this.Prompt('请输入名称','确认','text').then(res =>{ // do something use res }); },
Related recommendations:
How to make a pop-up window effect with jQuery
in H5 How to solve the problem that the pop-up window cannot pop up using webview
How to use js to pass parameters to the pop-up window in php
The above is the detailed content of VUE2 event-driven pop-up window implementation example. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

Implementing exact division operations in Golang is a common need, especially in scenarios involving financial calculations or other scenarios that require high-precision calculations. Golang's built-in division operator "/" is calculated for floating point numbers, and sometimes there is a problem of precision loss. In order to solve this problem, we can use third-party libraries or custom functions to implement exact division operations. A common approach is to use the Rat type from the math/big package, which provides a representation of fractions and can be used to implement exact division operations.
