vue提示彈窗插件(alert畸写、confirm驮瞧、msg)

使用Vue開發(fā)的過程中,我們會(huì)在很多地方用到一些同樣的功能枯芬,比如說提示彈窗论笔,這種時(shí)候我們可以自定義一個(gè)插件,方便在多個(gè)地方使用千所。

項(xiàng)目目錄結(jié)構(gòu):

image.png

-modules:放置模塊的文件夾狂魔,里面有一個(gè) alert 文件夾,用于存放 alert 插件 淫痰;
-Alert.vue:就是我們要在多處用到提示彈窗組件最楷;
-index.js:對(duì)于該自定義插件的一些配置;

代碼編寫:

Alert.vue
<template>
  <!-- 初始狀態(tài)下隱藏提示框 -->
  <div v-show="isShow">
    <div class="alert" :class="type">
      <div class="flex" >{{msg}}</div>
      <!-- alert插件只顯示確定按鈕 -->
      <div class="space-around" v-if="type === 'alert'">
        <div class="btnCommon success" @click="close">確定</div>
      </div>
      <!-- confirm插件顯示取消和確定按鈕 -->
      <div class="space-around" v-else-if="type === 'confirm'">
        <div class="btnCommon cancel" @click="cancelEvent">取消</div>
        <div class="btnCommon success" @click="successEvent">確定</div>
      </div>

    </div>
    <!-- 背景遮罩 -->
    <div class="mask" @click="closeMask" v-if="type !== 'msg'"></div>
  </div>
</template>

<script>
  export default {
    name: 'Alert',
    props: {
      // 提示信息
      msg: {
        type: String,
        default: ''
      },
      // 是否顯示提示框
      isShow: {
        type: Boolean,
        default: false
      },
      // 插件類型:alert/confirm
      type: {
        type: String,
        default: 'alert'
      },
      // confirm插件接收的確認(rèn)事件
      success: {
        type: Function,
        default: () => {
          console.log('點(diǎn)擊了success');
        }
      },
      // confirm插件接收的取消事件
      cancel: {
        type: Function,
        default: () => {
          console.log('點(diǎn)擊了cancel');
        }
      }
    },
    methods: {
      // 關(guān)閉彈窗
      close() {
        this.isShow = false
      },
      // alert 插件點(diǎn)擊陰影區(qū)域關(guān)閉彈窗
      closeMask() {
        if(this.type === 'alert') {
          this.close();
        }
      },
      // confirm 插件點(diǎn)擊取消觸發(fā)的事件
      cancelEvent() {
        this.cancel();
        this.close();
      },
      // confirm 插件點(diǎn)擊確定觸發(fā)的事件
      successEvent() {
        this.success();
        this.close();
      }
    },
    updated(){
      var _this = this;
      if(_this.type == 'msg'){
        setTimeout(function(){_this.close()},1500);
      }
    }
  }



// 調(diào)用實(shí)例
//    this.$alert('測(cè)試')
//    this.$confirm('測(cè)試Confirm', () => {
//      console.log('這是確定事件');
//    }, () => {
//      console.log('這是取消事件');
//    })
//    this.$msg('測(cè)試')

</script>
<style lang="stylus" rel="stylesheet/stylus">
  .alert {
    width: 500px;
    height: auto;
    position: fixed;
    left: 50%;
    margin-left: -250px;
    top: 50%;
    margin-top: -75px;
    background-color: #fff;
    border-radius: 15px;
    box-shadow: 0 5px 8px rgba(0, 0, 0, .05);
    z-index: 3000;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .msg{
    background-color: rgba(0, 0, 0, 0);
    box-shadow:none;
  }
  .msg .flex{
      padding: 20px 40px!important;
      background-color: #fff;
      border-radius :10px;
      box-shadow: 10px 10px 18px rgba(0, 0, 0, .2);
    }

  .flex {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 30px;
    word-break: break-all;
    line-height:40px;
  }
  .space-around {
    display: flex;
    justify-content: space-around;
    align-items: center;
    width: 100%;
    border-top:1px solid #cfcfcf;
  }

  .btnCommon {
    width: 250px;
    height: 92px;
    line-height: 92px;
    text-align: center;
    border-radius: 6px;
  &.success {
     background-color: $btnMain;
     color: #EC736F;
     cursor: pointer;
  &:hover {
     background-color: $btnDark;
   }
  }
  &.cancel {
      width: 249px;
      color: #333;
      cursor: pointer;
      border-right: 1px solid #cfcfcf;
    }
  }

  .mask {
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, .4);
    left: 0;
    top: 0;
    overflow: hidden;
    z-index: 2000;
  }
  .msg .mask{
    display: none;
  }
</style>
index.js
/**
 * Created by ZD on 2020/11/3.
 */
import AlertComponent from './Alert.vue';

const Alert = {}

// Vue暴露了一個(gè)install方法,用于自定義插件
Alert.install = function (Vue) {
  // 創(chuàng)建一個(gè)子類
  const AlertConstructor = Vue.extend(AlertComponent);
  // 實(shí)例化這個(gè)子類
  const instance = new AlertConstructor();
  // 創(chuàng)建一個(gè)div元素籽孙,并把實(shí)例掛載到div元素上
  instance.$mount(document.createElement('div'));
  // 將el插入到body元素中
  document.body.appendChild(instance.$el);

  // 添加實(shí)例方法
  // msg插件的實(shí)例方法:只接收提示信息msg
  Vue.prototype.$msg = msg => {
    instance.type = 'msg';
    instance.msg = msg;
    instance.isShow = true;
  };
  // alert插件的實(shí)例方法:只接收提示信息msg
  Vue.prototype.$alert = msg => {
    instance.type = 'alert';
    instance.msg = msg;
    instance.isShow = true;
  };
  // confirm插件的實(shí)例方法烈评,可以接收三個(gè)參數(shù)
  // msg:提示信息
  // success:點(diǎn)擊確定執(zhí)行的函數(shù)
  // cancel:點(diǎn)擊取消執(zhí)行的函數(shù)
  Vue.prototype.$confirm = (msg, success, cancel) => {
    instance.type = 'confirm';
    instance.msg = msg;
    instance.isShow = true;
    if(typeof success !== 'undefined') {
      instance.success = success;
    }
    if(typeof cancel !== 'undefined') {
      instance.cancel = cancel;
    }
  }
}

export default Alert;
至此,我們的自定義插件就差最后點(diǎn)睛一筆了犯建,只需要使用 Vue.use 方法將插件安裝進(jìn)去即可讲冠。
main.js
import Vue from 'vue'
import App from './App.vue'
import alert from './modules/alert'
 
Vue.config.productionTip = false
Vue.use(alert)  // 注意适瓦,Vue.use方法必須在new Vue之前被調(diào)用
 
new Vue({
  render: h => h(App),
}).$mount('#app')

使用方法:

App.vue
<template>
  <div id="app">
    <button @click="handleAlert">alert</button>
    <button @click="handleConfirm">confirm</button>
    <button @click="handleMsg">alert</button>
  </div>
</template>
 
<script>
 
export default {
  name: 'App',
  methods: {
    handleAlert() {
      this.$alert('測(cè)試')
    },
    handleConfirm() {
      this.$confirm('測(cè)試Confirm', () => {
        console.log('這是確定事件');
      }, () => {
        console.log('這是取消事件');
      })
    },
    handleMsg() {
      this.$msg('測(cè)試')
    }
  }
}
</script>

頁(yè)面效果

1.alert


image.png

2.confirm


image.png

3.msg
image.png
此文章在原作者插件基礎(chǔ)上增加了msg,顯示時(shí)間可自行設(shè)定

原文https://blog.csdn.net/sinat_40697723/article/details/106036056

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市嗦随,隨后出現(xiàn)的幾起案子称杨,更是在濱河造成了極大的恐慌姑原,老刑警劉巖呜舒,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袭蝗,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡朵逝,警方通過查閱死者的電腦和手機(jī)乡范,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門晋辆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人芋膘,你說我怎么就攤上這事为朋。” “怎么了埃儿?”我有些...
    開封第一講書人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵融涣,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我剃斧,道長(zhǎng)忽你,這世上最難降的妖魔是什么科雳? 我笑而不...
    開封第一講書人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任糟秘,我火速辦了婚禮,結(jié)果婚禮上散庶,老公的妹妹穿的比我還像新娘凌净。我一直安慰自己,他們只是感情好须教,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開白布没卸。 她就那樣靜靜地躺著秒旋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪煤蚌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評(píng)論 1 300
  • 那天筒占,我揣著相機(jī)與錄音翰苫,去河邊找鬼这橙。 笑死,一個(gè)胖子當(dāng)著我的面吹牛埃唯,可吹牛的內(nèi)容都是我干的鹰晨。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼漠趁,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼闯传!你這毒婦竟也來了膝昆?” 一聲冷哼從身側(cè)響起叠必,我...
    開封第一講書人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤纬朝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后判没,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體隅茎,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辟犀,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了魂毁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡咬崔,死狀恐怖垮斯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情甚脉,我是刑警寧澤铆农,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布墩剖,位于F島的核電站,受9級(jí)特大地震影響郊霎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜书劝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一购对、第九天 我趴在偏房一處隱蔽的房頂上張望骡苞。 院中可真熱鬧,春花似錦解幽、人聲如沸烘苹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽然爆。三九已至,卻和暖如春奴烙,著一層夾襖步出監(jiān)牢的瞬間剖张,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工幅虑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留顾犹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓擎宝,卻偏偏與公主長(zhǎng)得像绍申,于是被迫代替她去往敵國(guó)和親极阅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容