vue封装一个简单的弹出框

<template>
  <div id="tip_alertModal">
    <div class="t-alert-mask"></div>
    <div class="t-alert-container">
      <div class="t-alert-title">
        <span>
          {{title}}
        </span>
        <img @click="close" src="../../../static/images/alert/guanbi.png" alt="">
      </div>
      <div class="t-alert-content">
        <span class="content-text">
          {{content}}
        </span>
      </div>
      <div class="t-alert-confirm">
        <button @click="confirm">确定</button>
        <!-- 默认是没有取消按钮的,data定义默认true false -->
        <button class="cancel-btn" v-show="cancelBtn" @click="cancel">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        show: true, // 通过这个属性,控制是否移除dom元素
        title:'', // 顶部标题
        content:'', // 内容
        cancelBtn: false // 取消按钮
      };
    },
    methods: {
      close() {
        // 右上角关闭
        this.a_close && this.a_close();
        this.show = false;
        // 删除判断增加的window属性
        delete window.alertIsShow;
      },
      confirm() {
        // 确定
        this.a_confirm && this.a_confirm();
        this.show = false;
        // 删除判断增加的window属性
        delete window.alertIsShow;
      },
      cancel() {
        // 取消
        this.a_cancel && this.a_cancel();
        this.show = false;
        // 删除判断增加的window属性
        delete window.alertIsShow;
      }
    },
    watch: {
      show(cur, old) {
      	// 通过监控data里的show属性  弹框有三个事件(右上角取消  确定按钮  取消按钮)
      	// 每个事件写了 this.show = false
      	// 当弹框出现的时候 点击任何一个事件  都会触发这里的监控事件  将页面上的弹框Dom移除
        if (cur === false) {
          let tip_alert = document.getElementById('tip_alertModal');
          tip_alert.parentNode.removeChild(tip_alert);
        }
      }
    }
  }
</script>

//定义一个js文件
import Vue from 'vue'; 
import Alert from '@/components/public/alertModal'; //引入刚才写的弹框组件 
let AlertConstructor = Vue.extend(Alert); // 返回一个“扩展实例构造器” 

let AlertModal = (o) => {
  let alertDom = new AlertConstructor({
    el: document.createElement('div'); //将Alert组件挂载到新创建的div上 
  })
  document.body.appendChild(alertDom.$el); //把Alert组件的dom添加到body里 
	
  // 标题
  alertDom.title = o.title || '信息';
  // 单条内容
  alertDom.content = o.content;
  // 关闭按钮
  alertDom.cancelBtn = o.cancelBtn;

  // 弹框三个事件 右上角关闭 确定 取消
  alertDom.a_close = o.close || null;
  alertDom.a_confirm = o.confirm || null;
  alertDom.a_cancel = o.cancel || null;

}
export default AlertModal;


main.js
import alert from '@/common/alertModal' //这里引入的是js文件 
Vue.prototype.$alert = alert;  



//在任意组件调用
<template>
  <div>
    <button @click="operate">点击调用弹框</button>
  </div>
</template>

<script>
export default {
  methods: {
    operate() {
      this.$alert({
        title: '信息',
        content: '登入成功!',
        cancelBtn: true, //这个是启用取消按钮,
        close() {
          // 这里执行点击右上角需要做的事,默认执行关闭弹框
        },
        confirm() {
          // 这里执行点击确定按钮需要做的事,默认执行关闭弹框
        },
        cancel() {
          // 这里执行点击取消按钮需要做的事,默认执行关闭弹框
        }
      })
    }
  }
}
</script>

控制一个页面只显示一个弹窗
methods: {
operate () {
if (!window.alertIsShow) {
// 弹框模板有个 delete window.alertIsShow 是为了弹框关闭之后能再次显示
this.$alert({
title: ‘信息’,
content: ‘登入成功!’,
cancelBtn: true,
close () {
// 这里执行点击右上角需要做的事,默认执行关闭弹框
},
confirm () {
// 这里执行点击确定按钮需要做的事,默认执行关闭弹框
},
cancel () {
// 这里执行点击取消按钮需要做的事,默认执行关闭弹框
}
})
window.alertIsShow = true;
}
}
}

less

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我给您提供一个简单Vue出框组件,您可以自行进行封装和修改: 1. 首先在Vue项目的src/components目录下创建一个MessageBox.vue文件,用于封装出框组件。 2. 在MessageBox.vue文件中定义组件模板和样式: ``` <template> <div class="message-box" v-show="visible"> <div class="message-title">{{ title }}</div> <div class="message-content">{{ content }}</div> <div class="message-buttons"> <button @click="confirm">确认</button> <button @click="cancel">取消</button> </div> </div> </template> <style> .message-box { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; background-color: #fff; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); z-index: 9999; } .message-title { font-size: 20px; font-weight: bold; padding: 10px; border-bottom: 1px solid #ccc; } .message-content { font-size: 16px; padding: 20px; } .message-buttons { text-align: center; margin-top: 20px; } .message-buttons button { margin-right: 10px; padding: 5px 10px; border: none; border-radius: 3px; background-color: #ccc; color: #fff; cursor: pointer; } .message-buttons button:hover { background-color: #333; } </style> ``` 在组件中,我们定义了一个div元素,用于显示出框,以及一些样式属性。 3. 在MessageBox.vue文件中定义组件的props和methods: ``` <script> export default { name: 'MessageBox', props: { title: { type: String, default: '提示' }, content: { type: String, default: '' } }, data() { return { visible: true } }, methods: { confirm() { this.$emit('confirm') this.visible = false }, cancel() { this.$emit('cancel') this.visible = false } } } </script> ``` 在props中定义了组件的title和content属性,默认值为'提示'和''。在data中定义了visible属性,用于控制出框的显示和隐藏。在methods中定义了confirm和cancel方法,用于响应用户点击出框上的确认和取消按钮,并通过$emit方法向父组件传递事件。 4. 在需要使用出框的页面中,引入MessageBox组件,并在模板中使用: ``` <template> <div> <button @click="showMessage">显示出框</button> <message-box v-if="show" title="提示" :content="message" @confirm="confirm" @cancel="cancel"></message-box> </div> </template> <script> import MessageBox from '@/components/MessageBox' export default { components: { MessageBox }, data() { return { show: false, message: '' } }, methods: { showMessage() { this.show = true this.message = '确定要删除这个文件吗?' }, confirm() { console.log('确认删除文件') }, cancel() { console.log('取消删除文件') } } } </script> ``` 在模板中,我们定义了一个按钮,用于触发出框的显示。当用户点击按钮时,show属性会被设置为true,出框就会显示出来。同时,我们还将MessageBox组件作为子组件引入,通过props传递参数和事件。当用户点击出框上的确认和取消按钮时,confirm和cancel方法会被执行。 希望这个例子可以帮到您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值