vue中關(guān)于confirm確認(rèn)框的用法
vue中confirm確認(rèn)框用法
示例圖片

示例代碼
this.$confirm("是否確認(rèn)該操作","提示",{
iconClass: "el-icon-question",//自定義圖標(biāo)樣式
confirmButtonText: "確認(rèn)",//確認(rèn)按鈕文字更換
cancelButtonText: "取消",//取消按鈕文字更換
showClose: true,//是否顯示右上角關(guān)閉按鈕
type: "warning",//提示類(lèi)型 success/info/warning/error
}).then(()=>{
//確認(rèn)操作
}).then((data) => {
//取消操作
}).catch((err) => {
//捕獲異常
console.log(err);
});vue自定義$confirm彈窗確認(rèn)組件
1.Vue.extend()
使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類(lèi)”。參數(shù)是一個(gè)包含組件選項(xiàng)的對(duì)象
.vue單文件經(jīng)過(guò)webpack打包之后是一個(gè)組件示例對(duì)象,因此可以傳到Vue.extend中生成一個(gè)包含此組件的類(lèi)
2.new VueComponent().$mount()
new VueComponent() 創(chuàng)建實(shí)例,調(diào)用$mount()可以手動(dòng)編譯
如果.$mount(’#app’)有參數(shù),表示手動(dòng)編譯并且掛載到該元素上。
3.$el屬性 類(lèi)型:string | HTMLElement
手動(dòng)編譯后的示例對(duì)象中存在一個(gè)$el對(duì)象(dom元素),可以作為模板被插入到頁(yè)面中。
4.Vue.prototype 添加 Vue 實(shí)例方法
源碼
- 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;- 全局注冊(cè)
import confirm from './views/components/message/confirm' Vue.prototype.$confirm = confirm;
- 使用
this.$confirm({
? ? title: '',
? ? msg: '模式未保存,確定離開(kāi)?',
? ? yesBtnText: "離開(kāi)"
}).then(() => {
? ? console.log('yes')
? ? })
? ?.catch(() => {
? ? console.log('cancel')
});總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3.0+element表格獲取每行數(shù)據(jù)代碼示例
這篇文章主要給大家介紹了關(guān)于vue3.0+element表格獲取每行數(shù)據(jù)的相關(guān)資料,在element-ui中,你可以通過(guò)為表格的行綁定單擊事件來(lái)獲取表格中的一行數(shù)據(jù),需要的朋友可以參考下2023-09-09
vue項(xiàng)目中路由懶加載的三種方式(簡(jiǎn)潔易懂)
本文主要介紹了vue項(xiàng)目中路由懶加載的三種方式,主要包括vue異步組件,組件懶加載,webpack的require.ensure(),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
vue初嘗試--項(xiàng)目結(jié)構(gòu)(推薦)
這篇文章主要介紹了vue初嘗試--項(xiàng)目結(jié)構(gòu)的相關(guān)知識(shí),需要的朋友可以參考下2018-01-01
基于Vue和Element-Ui搭建項(xiàng)目的方法
這篇文章主要介紹了基于Vue和Element-Ui搭建項(xiàng)目的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
解決vue2.0動(dòng)態(tài)綁定圖片src屬性值初始化時(shí)報(bào)錯(cuò)的問(wèn)題
下面小編就為大家分享一篇解決vue2.0動(dòng)態(tài)綁定圖片src屬性值初始化時(shí)報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

