最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

VUE2實(shí)現(xiàn)事件驅(qū)動(dòng)彈窗示例

 更新時(shí)間:2017年10月21日 16:40:48   作者:ermu  
本篇文章主要介紹了VUE2實(shí)現(xiàn)事件驅(qū)動(dòng)彈窗示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

前幾天想了解vue如何寫(xiě)彈窗組件

有以下兩種可取的寫(xiě)法:

1.狀態(tài)管理 如果彈窗組件放在根組件,使用vuex來(lái)管理組件的show和hide。放在組件內(nèi),通過(guò)增加v-show或v-if來(lái)控制,可結(jié)合slot,定義不同需求的彈窗

2.事件管理 注冊(cè)一個(gè)全局事件來(lái)打開(kāi)彈窗,傳入需展示的文字和相關(guān)的邏輯控制,可結(jié)合promise,實(shí)現(xiàn)異步

覺(jué)得對(duì)用像confirme和propmt這類彈窗,還是事件驅(qū)動(dòng)的好。最好就是能使用promise回調(diào)。

于是手癢就寫(xiě)了一個(gè)。下面是代碼。

propmt.js

import Vue from 'vue'
import promptComponent from './prompt.vue' // 引入彈窗的vue文件
const promptConstructor = Vue.extend(promptComponent); // 注冊(cè)組件
let instance = new promptConstructor().$mount(''); // 獲得組件的實(shí)例

document.body.appendChild(instance.$el); // 將組件的element插入到body中
const Alert = (text,okText)=>{
  if(instance.show === true) { //防止多次觸發(fā)
    return;
  }
  // 為彈窗數(shù)據(jù)賦值
  instance.show = true; 
  instance.isAlert = true;
  instance.okText = okText||'確定';
  instance.message = text;
  //返回一個(gè)promise對(duì)象,并為按鈕添加事件監(jiān)聽(tīng)
  return new Promise(function(resolve,reject) {
    instance.$refs.okBtn.addEventListener('click',function() {
      instance.show = false;
      resolve(true);
    })
  })
};
const Confirm = (text,okText,cancelText)=>{
  if(instance.show === true) {
    return;
  }
  instance.show = true;
  instance.okText = okText||'確定';
  instance.cancelText = cancelText||'取消';
  instance.message = text;
  return new Promise(function(resolve,reject) {
    instance.$refs.cancelBtn.addEventListener('click',function() {
      instance.show = false;
      resolve(false);
    });
    instance.$refs.okBtn.addEventListener('click',function() {
      instance.show = false;
      resolve(true);
    })
  })
};
const Prompt = (text,okText,inputType, defaultValue)=>{
  if(instance.show === true) {
    return;
  }
  instance.show = true;
  instance.isPrompt = true;
  instance.okText = okText||'確定';
  instance.message = text;
  instance.inputType = inputType || 'text';
  instance.inputValue = defaultValue || '';
  return new Promise(function(resolve,reject) {
    instance.$refs.okBtn.addEventListener('click',function() {
      instance.show = false;
      resolve(instance.inputValue);
    })
  })
};

export {Alert,Confirm,Prompt}

prompt.vue

<style lang="less" scoped>
  .confirm-enter-active {
    transition: all .2s;
  }

  .confirm-leave-active {
    transition: opacity .2s;
  }

  .confirm-leave-to {
    opacity: 0;
  }

  .confirm-enter {
    opacity: 0;
  }

  .confirm {
    position: relative;
    font-family: PingFangSC-Regular;
    font-size: 17px;
    -webkit-user-select: none;
    user-select: none;
    // 遮罩層樣式
    .masker {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, .4);
      -webkit-transition: opacity .1s linear;
      transition: opacity .1s linear;
      z-index: 100;
    }
    // 入庫(kù)數(shù)據(jù)錯(cuò)誤樣式
    .box {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 72%;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      text-align: center;
      border-radius: 12px;
      background-color: #fff;
      .message {
        height: 97px;
        line-height: 24px;
        font-family: PingFangSC-Regular;
        font-size: 17px;
        vertical-align: middle;
        color: #999;
        letter-spacing: -0.41px;
        p {
          margin: 20px auto 0 auto;
          vertical-align: middle;
        }
        &::after {
          content: '';
          height: 100%;
        }
      }
      .prompt {
        margin: 20px 0;
        width: 100%;
        p {
          margin: 5px auto;
          font-size: 17px;
          line-height: 24px;
        }
        input {
          margin: 5px auto;
          border: 1px solid #333;
          border-radius: 6px;
          width: 100px;
          height: 30px;
          font-size: 14px;
          line-height: 20px;
          text-align: center;
        }
      }
      .button-group {
        a {
          width: calc(50% - 0.5px);
          text-align: center;
          font-size: 17px;
          line-height: 43px;
          color: blue;
        }
        .max-width {
          width: 100% !important;;
        }
      }
    }
  }
</style>
<template>
  <transition name="confirm">
    <div class="confirm" v-show="show">
      <div class="masker" @touchmove.prevent>
        <div class="box">
          <div class="message" v-if="!isPrompt">
            <p>{{message}}</p>
          </div>
          <div class="prompt" v-if="isPrompt">
            <p>{{message}}</p>
            <input type="text" v-model="inputValue" v-if="inputType === 'text'" ref="inputEl">
            <input type="tel" v-model.number="inputValue" @keydown="enterCheck" v-if="inputType === 'tel'" ref="inputEl">
          </div>
          <div class="button-group clearfix bd-top">
            <a class="bd-right fl" ref="cancelBtn" v-show="!isAlert && !isPrompt">{{cancelText}}</a>
            <a class="fr" ref="okBtn" :class="{'max-width': isAlert || isPrompt}">{{okText}}</a>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>
<script type="text/ecmascript-6">
  import {mapState} from 'vuex'
  export default{
    data() {
      return {
        show: false,
        message: '請(qǐng)輸入提示語(yǔ)',
        okText: '確定',
        cancelText: '取消',
        isAlert: false,
        isPrompt: false,
        inputValue: '',
        inputType: ''
      }
    },
    methods: {
      // 金額輸入框校驗(yàn)
      enterCheck(event) {
        // 只允許輸入數(shù)字,刪除鍵,11位數(shù)字
        if (event.keyCode === 46 || event.keyCode === 8) {
          return;
        }
        if (event.keyCode < 47 || event.keyCode > 58 || event.keyCode === 190) {
          event.returnValue = false;
        }
      },
    },
    watch: {
      show(){
        if (this.show) {
          this.$nextTick(() => {
            console.log(this.$refs.inputEl);
            console.log(this.inputType);
            this.$refs.inputEl.focus();
          });
        }
      }
    }
  }
</script>

main.js

import {Alert,Prompt,Confirm} from '../lib/components/prompt/prompt.js'

Vue.prototype.Alert = function(text,okText) {
  return Alert(text,okText)
};
Vue.prototype.Confirm = function(text,okText,cancelText) {
  return Confirm(text,okText,cancelText)
};
Vue.prototype.Prompt = function(text,okText,inputType, defaultValue) {
  return Prompt(text,okText,inputType, defaultValue)
};
component.vue:

inputName() {
  this.Prompt('請(qǐng)輸入名稱','確認(rèn)','text').then(res =>{
    // do something use res
  });
},

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3中provide和inject作用和場(chǎng)景

    Vue3中provide和inject作用和場(chǎng)景

    Vue3中provide和inject作用和場(chǎng)景是頂層組件向任意的底層組件傳遞數(shù)據(jù)和方法,實(shí)現(xiàn)跨層組件通信,本文通過(guò)實(shí)例介紹Vue3 provide和inject的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2023-11-11
  • vscode自定義vue模板的實(shí)現(xiàn)

    vscode自定義vue模板的實(shí)現(xiàn)

    這篇文章主要介紹了vscode自定義vue模板的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題

    解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題

    下面小編就為大家分享一篇解決vue頁(yè)面刷新或者后退參數(shù)丟失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問(wèn)題及解決方法

    vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問(wèn)題及解決方法

    這篇文章主要介紹了vue中使用iview自定義驗(yàn)證關(guān)鍵詞輸入框問(wèn)題及解決方法,本文通過(guò)實(shí)例結(jié)合代碼的形式給大家介紹解決方法,需要的朋友可以參考下
    2018-03-03
  • Vue.config.productionTip?=?false?不起作用的問(wèn)題及解決

    Vue.config.productionTip?=?false?不起作用的問(wèn)題及解決

    這篇文章主要介紹了Vue.config.productionTip?=?false為什么不起作用,本文給大家分析問(wèn)題原因解析及解決方案,需要的朋友可以參考下
    2022-11-11
  • Vue動(dòng)態(tài)組件與內(nèi)置組件淺析講解

    Vue動(dòng)態(tài)組件與內(nèi)置組件淺析講解

    閑話少說(shuō),我們進(jìn)入今天的小小五分鐘學(xué)習(xí)時(shí)間,前面我們了解了vue的組件,我們本文主要是講解vue的動(dòng)態(tài)組件和內(nèi)置組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 解決vue $http的get和post請(qǐng)求跨域問(wèn)題

    解決vue $http的get和post請(qǐng)求跨域問(wèn)題

    這篇文章主要介紹了解決vue $http的get和post請(qǐng)求跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能詳解

    Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能詳解

    這篇文章主要介紹了Vue + Node.js + MongoDB圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能,結(jié)合實(shí)例形式詳細(xì)分析了Vue + Node.js + MongoDB基于圖片上傳組件實(shí)現(xiàn)圖片預(yù)覽和刪除功能相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • vue3中的Proxy一定要用Reflect的原因解析

    vue3中的Proxy一定要用Reflect的原因解析

    Reflect可以操作對(duì)象使用, proxy可以代理對(duì)象,但沒(méi)有找到為啥有時(shí)一定要在Proxy代理方法中使用Reflect,這篇文章主要介紹了vue3中的Proxy一定要用Reflect的原因解析,需要的朋友可以參考下
    2023-04-04
  • Vue使用axios添加請(qǐng)求頭方式

    Vue使用axios添加請(qǐng)求頭方式

    這篇文章主要介紹了Vue使用axios添加請(qǐng)求頭方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評(píng)論

日土县| 澎湖县| 庆城县| 德兴市| 延安市| 平凉市| 洞头县| 乡宁县| 鄂温| 普兰店市| 邹平县| 陇西县| 邯郸市| 武汉市| 滨海县| 凤冈县| 炉霍县| 廊坊市| 鹤庆县| 常熟市| 东源县| 石阡县| 灵山县| 铁力市| 灵寿县| 定州市| 尤溪县| 新昌县| 阳曲县| 满洲里市| 仲巴县| 女性| 海门市| 奇台县| 孝昌县| 晋江市| 普安县| 辽宁省| 靖边县| 马公市| 张家界市|