Vue封装弹框组件(定义在原型上)

本文介绍了如何在Vue项目中封装一个全局弹框组件,并将其定义在原型上,避免污染全局作用域。步骤包括创建组件文件、在main.js中引入并挂载到原型,以及组件的使用方法。
摘要由CSDN通过智能技术生成

Vue封装弹框组件(定义在原型上)

如果需要设置 全局弹框,在main.js中,Vue实例化的代码里添加。
防止污染全局作用域,可以通过在原型上定义它们使其在每个Vue页面中可被调用。

下面是组件效果图

在这里插入图片描述

1.我们在components文件夹下创建/qaconfirm/qaconfirm.vue

<template>
 <div class="mack" v-if="isShow">
  <div class="tipbox">
   <div class="title font_b">{
  {text.title.trim()}}</div>
   <div class="mesg font_s">
     <p class="icon"><span class="font_family">&#xe63f;</span></p>   <!-- icon提示图标图片 -->
     <span>{
  {text.content.trim()}}</span>
   </div>
   <div class="footb tipbtn">
     <a-button type="primary"
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
首先,你需要在父组件中引入 Dialog 弹框组件,并在父组件的 template 中添加一个触发显示弹框的按钮。例如: ```vue <template> <div> <button @click="showDialog">显示弹框</button> <Dialog v-model="dialogVisible"></Dialog> </div> </template> ``` 在上面的代码中,我们添加了一个按钮,当用户点击该按钮时,会触发 showDialog 方法。同时,我们引入了一个名为 Dialog 的组件,并将它的显示与隐藏与一个名为 dialogVisible 的变量绑定了起来。 接下来,在父组件的 script 中,我们需要定义 showDialog 方法以及 dialogVisible 变量。例如: ```vue <script> import Dialog from './Dialog.vue' export default { components: { Dialog }, data() { return { dialogVisible: false } }, methods: { showDialog() { this.dialogVisible = true } } } </script> ``` 在上面的代码中,我们首先引入了 Dialog 组件,然后在 data 中定义了 dialogVisible 变量,并将其默认值设置为 false。接着,我们定义了一个名为 showDialog 的方法,该方法将 dialogVisible 设置为 true,从而显示 Dialog 弹框组件。 最后,我们需要在 Dialog 弹框组件中添加一个关闭按钮,并将其与 dialogVisible 变量绑定起来。例如: ```vue <template> <div> <div class="dialog-mask" v-if="visible" @click="close"></div> <div class="dialog-wrapper" v-if="visible"> <div class="dialog-content"> <div class="dialog-header"> <h3 class="dialog-title">Dialog 标题</h3> <button class="dialog-close-btn" @click="close">×</button> </div> <div class="dialog-body"> <p>Dialog 内容</p> </div> </div> </div> </div> </template> <script> export default { props: { visible: { type: Boolean, required: true } }, methods: { close() { this.$emit('update:visible', false) } } } </script> ``` 在上面的代码中,我们添加了一个名为 close 的方法,该方法会将 dialogVisible 设置为 false,并通过 $emit 方法将更新后的值传递给父组件。同时,我们在弹框组件中添加了一个关闭按钮,并在按钮的 click 事件中调用 close 方法。 这样,当用户点击父组件中的按钮时,就会触发 showDialog 方法,从而显示 Dialog 弹框组件。当用户点击弹框中的关闭按钮时,会触发 close 方法,从而关闭弹框
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值