示例如下:
子组件:
1 <template> 2 <div> 3 <transition name="drop"> 4 <!--遮罩层开始--> 5 <div class="dialog-mask" v-show="dialogData.isShow"> 6 <!--对话框开始--> 7 <div class="dialog" id="dialog"> 8 <!--对话框标题开始--> 9 <div class="dialog-title modal-header"> 10 <p>{{dialogData.title}}</p> 11 <span class="close" @click="dialogData.btnText[1].method">×</span> 12 </div> 13 <!--对话框内容--> 14 <div class="dialog-content"> 15 <slot name="content"></slot> 16 </div> 17 <!--对话框底部--> 18 <div class="dialog-bottom"> 19 <button :class="index==0? 'btn btn-primary':'btn btn-default'" v-for="(btnt,index) in dialogData.btnText" @click="btnt.method">{{btnt.text}}</button> 20 </div> 21 </div> 22 <!--对话框结束--> 23 </div> 24 <!--遮罩层结束--> 25 </transition> 26 27 </div> 28 </template> 29 <style scoped> 30 .drop-enter-active,.drop-leave-active{ 31 transition: all .5s ease; 32 } 33 .drop-enter,.drop-leave-to{ 34 opacity: 0; 35 } 36 .dialog-mask{ 37 position: fixed; 38 left:0; 39 top:0; 40 width:100%; 41 height: 100%; 42 z-index: 1000; 43 background: rgba(0,0,0,0.2); 44 } 45 .dialog{ 46 width:50%; 47 height:50%; 48 position: fixed; 49 left:25%; 50 top:25%; 51 } 52 .dialog-title{ 53 width:100%; 54 height:18%; 55 border-top-left-radius:8px; 56 border-top-right-radius:8px; 57 display: table; 58 } 59 .dialog-title p{ 60 display: table-cell; 61 text-align: center; 62 vertical-align: middle; 63 } 64 .dialog-title span{ 65 position: absolute; 66 right: 3%; 67 } 68 .dialog-content{ 69 width:100%; 70 height:66%; 71 background: #fff; 72 } 73 .dialog-bottom{ 74 width:100%; 75 height:16%; 76 border-top:1px solid #E5E5E5; 77 border-bottom-left-radius:8px; 78 border-bottom-right-radius:8px; 79 background: #fff; 80 position: relative; 81 } 82 .btn{ 83 padding:.5% 3%; 84 } 85 .dialog-bottom >button:nth-child(1){ 86 position: absolute; 87 left:38%; 88 top:25%; 89 } 90 .dialog-bottom >button:nth-child(2){ 91 position: absolute; 92 left:50%; 93 top:25%; 94 } 95 </style> 96 <script> 97 export default{ 98 props:{ 99 'dialogData':{ 100 type:Object 101 } 102 103 } 104 } 105 </script>
父组件:
1 <template> 2 <div id="all"> 3 <my-dialog :dialogData = dialogData> 4 <div slot="content">我是对话框内容</div> 5 </my-dialog> 6 </div> 7 </template> 8 <style scoped> 9 #all{ 10 width:100%; 11 height:100%; 12 } 13 14 </style> 15 <script> 16 import myDialog from './dialog.vue' 17 18 export default{ 19 data(){ 20 return { 21 dialogData:{ 22 title:"提示", 23 isShow:true, 24 btnText:[ 25 {"text":"确定","method":this.sure}, 26 {"text":"取消","method":this.close} 27 ] 28 } 29 } 30 }, 31 methods:{ 32 sure(){ 33 alert("确定"); 34 }, 35 close(){ 36 alert("取消"); 37 this.dialogData.isShow = false; 38 } 39 }, 40 41 components: { 42 myDialog 43 } 44 } 45 </script>