Vue對話框組件使用方法詳解
本文實例為大傢分享瞭Vue對話框組件的使用,供大傢參考,具體內容如下
效果如下圖所示:(整體樣式模仿ant-design-vue Modal樣式,同時陰影覆蓋瀏覽器窗口)
①創建組件Dialog.vue:
<template> <div class="m-dialog-mask"> <div class="m-modal"> <div class="m-modal-content"> <div @click="onClose" class="u-close">✖</div> <div class="m-modal-header"> <div class="u-head">{{ title }}</div> </div> <div class="m-modal-body"> <p>{{ content }}</p> </div> <div class="m-modal-footer" v-show="footer"> <button class="u-cancel" @click="onCancel">{{ cancelText }}</button> <button class="u-confirm" @click="onConfirm">{{ okText }}</button> </div> </div> </div> </div> </template> <script> export default { name: 'Dialog', props: { title: { // 標題 type: String, default: '提示' }, content: { // 內容 type: String, default: '' }, cancelText: { // 取消按鈕文字 type: String, default: '取消' }, okText: { // 確認按鈕文字 type: String, default: '確定' }, footer: { // 是否顯示底部按鈕,默認顯示 type: Boolean, default: true } }, methods: { onClose () { this.$emit('close') }, onCancel () { this.$emit('cancel') }, onConfirm () { this.$emit('ok') } } } </script> <style lang="less" scoped> .m-dialog-mask { position: fixed; top: 0; right: 0; bottom: 0; left: 0; width: 100%; height: 100%; z-index: 1000000; background: rgba(0,0,0,0.45); .m-modal { width: 720px; position: relative; top: calc(50% - 240px); margin: 0 auto; .m-modal-content { position: relative; background: #fff; border-radius: 4px; box-shadow: 0 4px 12px rgba(0,0,0,.1); .u-close { position: absolute; top: 16px; right: 24px; color: rgba(0,0,0,.45); font-size: 18px; line-height: 22px; cursor: pointer; transition: color .3s; &:hover { color: rgba(0,0,0,.75); } } .m-modal-header { height: 22px; padding: 16px 24px; color: rgba(0,0,0,.65); border-radius: 4px 4px 0 0; border-bottom: 1px solid #e8e8e8; .u-head { margin: 0; color: rgba(0,0,0,.85); font-weight: 500; font-size: 16px; line-height: 22px; word-wrap: break-word; } } .m-modal-body { height: 324px; padding: 24px; font-size: 16px; line-height: 1.5; word-wrap: break-word; } .m-modal-footer { padding: 10px 16px; text-align: right; border-top: 1px solid #e8e8e8; .u-cancel { height: 32px; line-height: 32px; padding: 0 15px; font-size: 16px; border-radius: 4px; color: rgba(0,0,0,.65); background: #fff; border: 1px solid #d9d9d9; cursor: pointer; transition: all .3s cubic-bezier(.645,.045,.355,1); &:hover { color: #40a9ff; border-color: #40a9ff; } } .u-confirm { margin-left: 8px; height: 32px; line-height: 32px; padding: 0 15px; font-size: 16px; border-radius: 4px; background: #1890ff; border: 1px solid #1890ff; color: #fff; transition: all .3s cubic-bezier(.645,.045,.355,1); cursor: pointer; &:hover { color: #fff; background: #40a9ff; border-color: #40a9ff; } } } } } } </style>
②使用Dialog組件彈出對話框:
<Dialog title="Title" :content="content" :footer="true" cancelText="取消" okText="確認" @close="onClose" @cancel="onCancel" @ok="onConfirm" v-show="showDialog" /> import Dialog from '@/components/Dialog' components: { Dialog }, data () { return { showDialog: false, content: '', } }, methods: { onDialog (content) { // 調用Dialog彈出對話框 this.content = 'Content of the modal ...' this.showDialog = true }, onClose () { // 關閉dialog this.showDialog = false }, onCancel () { // “取消”按鈕回調 this.showDialog = false }, onConfirm () { // “確定”按鈕回調 this.showDialog = false } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。