您好,欢迎访问一九零五行业门户网

如何使用v-model与promise两种方式实现vue弹窗组件

这次给大家带来如何使用v-model与promise两种方式实现vue弹窗组件,使用v-model与promise两种方式实现vue弹窗组件的注意事项有哪些,下面就是实战案例,一起来看一下。
最近公司有一个后台业务虽然也是写在了现有的后台系统中,但是之后要为这个业务单独拉出来新建一个后台系统,所以现有的后台系统中的vue组件库,就不能用了(因为不知道将来的系统要基于什么组件库,以防给未来移植项目带来麻烦),这次业务中又遇到了弹窗的功能,所以只能手动写一个了(虽然说弹窗组件很简单,也是想自己总结一下,有不对的地方也请指出),一开始用传统的props,$emit但是觉得要接两个取消与确认的回调这块的逻辑分散了所以就用了promise两个回调的方式把两个回调写在了一起,并不一定好,算是提供一种思路吧。
一.概览
先看最后的调用方式
props $emit方式
<chat-modal ref="chat-modal" v-model="showmodal" canceltext="取消" suretext="确认" title="弹窗标题" small @on-ok="onok" @on-cancel="oncancel">   <p>slot的东西,想向弹窗中添加自定义的内容</p> </chat-modal> methods: {   display() {    this.showmodal = true;//交互点击手动触发显示弹窗    },   onok() {},//点击确认的回调   oncancel() {}//点击取消的回调 }
promise的回调方式
<chat-modal ref="chat-modal"></chat-modal> methods: {   display() {     this.$refs[chat-modal].openmodal({       title: 弹窗标题,       suretext: 确认,       canceltext: 取消     }).then(res => {       //点击确认的回调     }, res => {       //点击取消的回调     })   } }
第二种方式的好处就是把所有的逻辑都集中到了一个方法里。
二.看下组件的源码
tip: 样式有些烂...
<template>   <p>     <p class="shadow" v-show="showmodal"></p>     <p class="modal" :class="{'smsize': othertext.small || small}" v-show="showmodal">       <p class="header">{{ othertext.title || title}}</p>       <p class="body">         <slot></slot>       </p>       <p class="footer">         <p class="item success" id="sure" ref="sure" @click="makesure" v-show="othertext.suretext || suretext">{{ othertext.suretext || suretext }}</p>         <p class="item red" id="cancel" ref="cancel" @click="makecancel" v-show="othertext.canceltext || canceltext">{{ othertext.canceltext || canceltext }}</p>       </p>     </p>   </p> </template> <script> //此组件提供两种调用方法,可以在组件上v-model一个表示是否显示弹窗的对话框,然后需要的一些值通过props传入,然后$emit在组件上@监听做回调 //第二中方法所有的传值回调都只需要在组件内部的一个方法调用然后在组件外部this.$refs[xxx].open调用然后.then触发回调,比上一种方便些 var initothertext = {   suretext: ,   canceltext: ,   title: ,   small: false }; export default {   props: {     title: {       type: string     },     suretext: {       type: string     },     canceltext: {       type: string     },     value: {       type: boolean     },     small: {       type: boolean     }   },   watch: {     value(newval) {       this.showmodal = newval;     }   },   data() {     return {       othertext: json.parse(json.stringify(initothertext)),       showmodal: this.value     };   },   methods: {     makesure() {       this.$emit(on-ok);       this.$emit(input, false);     },     makecancel() {       this.$emit(on-cancel);       this.$emit(input, false);     },     openmodal(othertext) {       this.othertext = { ...othertext };       this.showmodal = true;       var pms = new promise((resolve, reject) => {         this.$refs[sure].addeventlistener(click, () => {           this.showmodal = false;           resolve(点击了确定);         });         this.$refs[cancel].addeventlistener(click, () => {           this.showmodal = false;           reject(点击了取消);         });       });       return pms;     }   } }; </script> <style lang="scss" scoped> .shadow {   background-color: rgba(0, 0, 0, 0.5);   display: table;   height: 100%;   left: 0;   position: fixed;   top: 0;   transition: opacity 0.3s ease;   width: 100%;   z-index: 50; } .modal {   display: table-cell;   vertical-align: middle;   overflow-x: hidden;   position: fixed;   background-color: white;   box-shadow: rgba(0, 0, 0, 0.33) 0px 2px 8px;   border-radius: 5px;   outline: 0px;   overflow: hidden;   transition: all 0.3s ease;   width: 600px;   height: 400px;   top: 50%;   left: 50%;   margin-top: -200px;   margin-left: -300px; } .header {   align-items: center;   background-color: #62a39e;   box-shadow: 0 1px 1px rgba(0, 0, 0, 0.16);   color: #fff;   font-weight: bold;   display: -ms-flexbox;   display: flex;   height: 3.5rem;   padding: 0 1.5rem;   position: relative;   z-index: 1; } .body {   align-items: center;   padding: 1.5rem; } .footer {   justify-content: flex-end;   padding: 1.5rem;   position: absolute;   bottom: 0;   width: 100%;   float: right; } .item {   color: white;   text-align: center;   border-radius: 5px;   padding: 10px;   cursor: pointer;   display: inline-block; } .info {   background-color: #2196f3; } .success {   background-color: #62a39e; } .red {   background-color: #e95358; } .smsize {   height: 200px; } </style>
首先分析一下第一种方式: 调用者需要在组件外部v-model上绑定一个变量(例中为showmodal)来指示弹窗是否显示,显示的时候需要在组件外部手动设置 this.showmodal = true ,组件内部props定义一个属性来接这个值为 value: {type: boolean} ,同时在组件内部在用声明一个变量用来同步外部传进来的props值 默认值为 showmodal: this.value (内部声明的值也叫了showmodal),在watch中监听进行同步 watch: { value(newval) { this.showmodal = newval } } ;然后把组件内部的这个showmodal值绑定在需要显示或者隐藏的dom元素上。向外抛出事件的时候是在点击组件内部的确定与关闭按钮时候
makesure() {       this.$emit(on-ok);       this.$emit(input, false);     }, makecancel() {       this.$emit(on-cancel);       this.$emit(input, false);     }
this.$emit(on-ok);this.$emit(on-cancel); 这两句的是向外抛出事件在组件外部@接一下然后写自己需要的回调函数。这时就可以实现弹窗的显示与隐藏了,你可能发现并没有一句代码去设置this.showmodal = false;弹窗就隐藏了。主要是因为这几句代码 v-model = 'showmodal' 和 组件内部的 props: {value: {type: boolean}} this.$emit(input, false) 。v-model其实是vue的语法糖, <chat-modal v-model="showmodal"> 其实可以写为 <chat-modal :value="showmodal" @input="showmodal = arguments[0]"> 所以要求我们在组件内部必须规定props的名字必须为value, 然后在组件内部触发确定或者取消的时候在组件内部触发 this.$emit(input, false) 这样实现了直接隐藏弹窗而不必打扰用户让用户在组件外部在手动将showmodal置为false.
然后来看promise的方式: 第一种方式传进来的值都通过props来接的,这种方式通过在组件内部定义了另一个对象来接传进来的值,
var initothertext = {   suretext: ,   canceltext: ,   title: ,   small: false }; othertext: json.parse(json.stringify(initothertext)),
然后在menthods里定义了一个名为openmodal的方法,然后把传进来的一系列参数赋值给组件内部的对象 this.othertext = { ...othertext }; this.showmodal = true; 并且将showmodal置为true,然后每次触发的时候新建一个promise对象,里面的异步事件为点击确定和取消的两个点击事件,这里要操作dom了
this.$refs[sure].addeventlistener(click, () => {   this.showmodal = false;   resolve(点击了确定); });
获取确定按钮的dom元素绑定点击事件,回调里将showmodal置为false并且resolve,
this.$refs[cancel].addeventlistener(click, () => {   this.showmodal = false;   reject(点击了取消); });
获取取消按钮的dom绑定点击事件,回调里reject.
遇到的坑
这之前遇到了一个坑,因为第一次已经绑定了点击事件,第二次resolve和reject就会失败,本想取消一下绑定事件,但是因为将整个弹窗v-show=showmodal的原因整个dom被display:none;了就不需要手动解绑了。 第二个是关于用v-if还是v-show来隐藏弹窗,一开始用的是v-if但是发现在这步时
this.showmodal = true; var pms = new promise((resolve, reject) => {   this.$refs[sure].addeventlistener.xxx//省略 }); return pms;
将showmodal置为true时然后就去绑定事件这时候还没有dom还没有解析玩dom树上还没有,要不就得用this.$nexttick增加了复杂度,最后采用了v-show;
关于优先级问题
如果既在组件上用prop传了值(title,suretext之类的)如 <chat-modal" title="xx" suretext="xxx"></chat-modal> 也在方法里传了
this.$refs[chat-modal].openmodal({   title: 服务小结,   suretext: 提交并结束,   canceltext: 取消   }).then();
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何使用vue内父子组件通讯todolist组件
如何使用vue整合adminlte模板
以上就是如何使用v-model与promise两种方式实现vue弹窗组件的详细内容。
其它类似信息

推荐信息