vue自定义$confirm弹窗确认组件

前言

1.Vue.extend()

使用基础 Vue 构造器,创建一个“子类”。参数是一个包含组件选项的对象
.vue单文件经过webpack打包之后是一个组件示例对象,因此可以传到Vue.extend中生成一个包含此组件的类

2.new VueComponent().$mount()

new VueComponent() 创建实例,调用$mount()可以手动编译

如果.$mount(’#app’)有参数,表示手动编译并且挂载到该元素上。

3.$el属性 类型:string | HTMLElement

手动编译后的示例对象中存在一个$el对象(dom元素),可以作为模板被插入到页面中。

4.Vue.prototype 添加 Vue 实例方法

源码

vue文件

<template>
<div :class="{'pop-up':true,'show':show}">
    <div class="popup-mask" v-if="hasMark"></div>
    <transition name="bottom">
        <div class="popup-note bottom">
            <div class="pop-content">
                <div class="pop-tit">
                    {{title}}
                </div>
                <p class="pop-note hasTitle">
                    <span class="msg" v-html="msg"></span>
                </p>
                <div class="btn-wrapper" v-if="type == 'alert'" @click.stop="alertClick">
                    <span class="btn btn-block yes-btn">{{alertBtnText}}</span>
                </div>
                <div class="btn-wrapper" v-if="type == 'confirm'">
                    <span @touchstart.prevent="noClick" class="btn">{{noBtnText}}</span>
                    <span @touchstart.prevent="yesClick" class="btn yes-btn">{{yesBtnText}}
		    </span>
                </div>
            </div>
        </div>
    </transition>
</div>
</template>
<script>
export default {
    props: {
        title: {
            type: String,
            default: '提示'
        },
        msg: {
            type: String,
            default: ''
        },
        type: {
            type: String,
            default: 'alert'
        },
        alertBtnText: {
            type: String,
            default: '我知道了'
        },
        yesBtnText: {
            type: String,
            default: '确定'
        },
        noBtnText: {
            type: String,
            default: '取消'
        },
        hasMark: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            promiseStatus: null,
            show: false
        }
    },
    methods: {
        confirm() {
            let _this = this;
            this.show = true;
            return new Promise(function (resolve, reject) {
                _this.promiseStatus = {resolve, reject};
            });
        },
        noClick() {
            this.show = false;
            this.promiseStatus && this.promiseStatus.reject();
            
        },
        yesClick() {
            this.show = false;
            this.promiseStatus && this.promiseStatus.resolve();
            
        },
        alertClick() {
            this.show = false;
            this.promiseStatus && this.promiseStatus.resolve();
        }
    }
}
</script>


<style lang='less'>
@import "../../../static/less/components/tip/index.less";
</style>

confirm.js

import Vue from 'vue'
import message from './message.vue'
const VueComponent = Vue.extend(message);
const vm = new VueComponent().$mount();
let init = false;
let defaultOptions = {
yesBtnText: '确定',
noBtnText: '取消'
};

const confirm = function (options) {
Object.assign(vm,defaultOptions , options,{
    type:'confirm'
});

if (!init) {
    document.body.appendChild(vm.$el);
    init = true;
}

return vm.confirm();
};

export default confirm;

全局注册

import confirm from './views/components/message/confirm'
Vue.prototype.$confirm = confirm;

使用

this.$confirm({
    title: '',
    msg: '模式未保存,确定离开?',
    yesBtnText: "离开"
}).then(() => {
    console.log('yes')
    })
   .catch(() => {
    console.log('cancel')
});
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以使用Element UI的Dialog组件来自定义三个按钮的confirm。 首先,在需要使用confirm组件中引入Dialog组件和Button组件: ```javascript import { Dialog, Button } from 'element-ui'; ``` 然后,在该组件的methods中定义一个方法,用于自定义confirm: ```javascript methods: { myConfirm() { Dialog.confirm({ title: '提示', message: '确定要执行该操作吗?', customClass: 'my-confirm', showCancelButton: true, showConfirmButton: false, showClose: false, cancelButtonText: '取消', cancelButtonClass: 'my-cancel-button', closeOnClickModal: false, closeOnPressEscape: false, beforeClose: (action, instance, done) => { if (action === 'cancel') { done(); } else { // 执行确认操作 done(); } } }); } } ``` 在myConfirm方法中,使用Dialog.confirm方法来创建一个confirm。通过传递一些参数,可以自定义的样式和功能: - title:标题 - message:内容 - customClass:自定义类名 - showCancelButton:是否显示取消按钮 - showConfirmButton:是否显示确认按钮 - showClose:是否显示关闭按钮 - cancelButtonText:取消按钮文本 - cancelButtonClass:自定义取消按钮类名 - closeOnClickModal:是否允许点击外部关闭 - closeOnPressEscape:是否允许按下ESC键关闭 - beforeClose:关闭前的回调函数,可以在其中执行确认操作 在beforeClose回调函数中,可以根据用户操作的结果来执行相应的操作,例如调用API接口执行删除操作。 最后,在页面中使用Button组件来触发myConfirm方法即可: ```html <template> <Button type="warning" @click="myConfirm">删除</Button> </template> ``` 这样就可以自定义三个按钮的confirm了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萝卜砸大坑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值