首页 / VUE / VUE2事件驱动弹窗实现示例
VUE2事件驱动弹窗实现示例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了VUE2事件驱动弹窗实现示例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5393字,纯文字阅读大概需要8分钟。
内容图文
本文主要介绍了VUE2实现事件驱动弹窗示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。前几天想了解vue如何写弹窗组件
有以下两种可取的写法:
1.状态管理 如果弹窗组件放在根组件,使用vuex来管理组件的show和hide。放在组件内,通过增加v-show或v-if来控制,可结合slot,定义不同需求的弹窗
2.事件管理 注册一个全局事件来打开弹窗,传入需展示的文字和相关的逻辑控制,可结合promise,实现异步
觉得对用像confirme和propmt这类弹窗,还是事件驱动的好。最好就是能使用promise回调。
于是手痒就写了一个。下面是代码。
propmt.js
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
相关推荐:
jQuery怎么做出弹窗效果
H5中的弹窗无法用webview弹出怎么解决
在php中怎么利用js把参数传递给弹窗
以上就是VUE2事件驱动弹窗实现示例的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的VUE2事件驱动弹窗实现示例全部内容,希望文章能够帮你解决VUE2事件驱动弹窗实现示例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。