vue 弹框

本文介绍了如何在Vue.js中创建并使用弹框组件,包括组件的代码实现和在HTML及JS中的调用方法。

弹框

一、组件代码

<template>
  <div class="MisDialog">
    <el-dialog
      v-if="dialogVisible"
      :custom-class="className"
      :width="`${width}px`"
      :fullscreen="fullscreen"
      top="24vh"
      :visible.sync="dialogVisible"
      :title="title"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :before-close="beforeClose"
    >
      <slot />
      <template slot="footer">
        <slot
          name="footer"
          class="dialog-footer"
        />
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'MisDialog',
  props: {
    title: { // 标题
      type: String,
      default: '弹框'
    },
    className: {
      type: String,
      default: 'mis-dialog'
    },
    width: { // 弹框宽度
      type: Number,
      default: 664
    },
    fullscreen: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      dialogVisible: false // 弹框的显示
    }
  },
  watch: {
    dialogIsVisible (newV, oldV) {
      this.dialogVisible = newV
    }
  },
  methods: {
    beforeClose () {
      this.$emit('close')
      // this.dialogVisible = false
    },
    show () {
      this.dialogVisible = true
    },
    hide () {
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
$td-height: 50px; // 行高

.MisDialog {
	::v-deep {
		.el-dialog {
			@include scrollbar(0, 8px, #dddddd, #888888);
			.el-dialog__body {
				max-height: 66vh;
				overflow: auto;
				$td-height: 40px; // 行高
				padding: 30px;
				overflow-y: overlay;
				.el-table {
					.el-table__row {
						td {
							height: $td-height;
							line-height: $td-height;
							padding: 0px;
						}
					}
				}
			}
			.el-dialog__footer {
				padding: 20px 30px;
			}
		}
		.el-pagination {
			$pagination-size: 28px;
			li {
				min-width: $pagination-size !important;
				min-height: $pagination-size;
				line-height: $pagination-size;
			}

			button {
				width: $pagination-size;
				height: $pagination-size;
				min-width: $pagination-size;
			}
		}
		.dialog-footer {
			.cc-btn-x {
				border-color: #eeeeee;
				color: #555555;
				&:hover {
					box-shadow: none;
					border-color: #dddddd;
					background-color: #fafafa;
				}
			}
		}
	}
}
</style>

二、调用

HTML调用

<mis-dialog
  ref="addDialog"
  :title="ccDialog.title"
  :width="694"
  @close="closeDig"
>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        @click="closeDig"
      >取 消</el-button>
      <el-button
        v-waves
        :loading="dialogOKLoading"
        class="cc-btn"
      >确 定</el-button>
    </span>
</mis-dialog>

JS调用

// 打开弹框
this.$refs['addDialog'].show()

// 关闭弹框
closeDig () { // 弹框关闭方法
    this.$refs['addDialog'].hide()
}

### 如何在 Vue.js 中创建和使用组件 #### 创建自定义组件 为了实现在 JavaScript 文件中通过 `this.xxx` 的方式调用功能,可以通过以下方法实现: 1. **编写组件** 首先,创建一个名为 `MessageBox.vue` 的组件文件。该组件负责渲染的内容。 ```vue <template> <div v-if="isDialog" class="message-box"> <h3>{{ title }}</h3> <p>{{ content }}</p> <button @click="close">关闭</button> </div> </template> <script> export default { name: "MessageBox", data() { return { isDialog: false, title: "", content: "" }; }, methods: { close() { this.isDialog = false; } } }; </script> <style scoped> .message-box { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; border: 1px solid #ccc; } </style> ``` 此部分实现了基本的逻辑[^5]。 --- 2. **注册全局方法** 接下来,在项目的入口文件(如 `main.js` 或其他初始化脚本)中引入上述组件,并将其挂载到 Vue 原型链上以便全局访问。 ```javascript import Vue from 'vue'; import MessageBoxComponent from './components/MessageBox.vue'; const MessageBoxConstructor = Vue.extend(MessageBoxComponent); const instance = new MessageBoxConstructor(); instance.$mount(); document.body.appendChild(instance.$el); Vue.prototype.$messageBox = (options) => { Object.assign(instance, options); instance.isDialog = true; return new Promise((resolve, reject) => { instance.closeCallback = () => { resolve(true); instance.isDialog = false; }; }); }; ``` 这段代码完成了将组件实例化并绑定至原型链的操作][^[^34]。 --- 3. **在任意地方调用** 完成以上配置后,可以在任何 Vue 组件的方法中直接调用 `$messageBox` 方法来显示。 ```javascript methods: { showCustomMessage() { this.$messageBox({ title: "提示", content: "这是一个自定义的消息" }) .then(() => console.log("用户已关闭")) .catch(console.error); } } ``` 这一步展示了如何通过 JavaScript 动态触发。 --- 4. **集成 Element UI 示例对比** 如果希望快速实现类似的效果而不自行开发,则可以直接借助成熟的组件库,比如 Element UI 提供的 `$message` 和对话 API。 ```html <template> <div> <el-button type="primary" @click="showElementUIAlert">点击我</el-button> </div> </template> <script> export default { methods: { showElementUIAlert() { this.$message.success("这是来自 Element UI 的成功消息"); } } }; </script> ``` 此处演示了基于现有架简化开发流程的方式[^2]。 --- #### 总结 无论是手动构建还是依赖第三方工具,都可以灵活满足项目需求中的交互设计目标。对于追求个性化样式与行为控制的应用场景而言,前者更具扩展性和定制价值;而对于注重效率及稳定性的小规模应用来说,后者无疑是更优的选择之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值