溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue如何實(shí)現(xiàn)一個(gè)彈窗插件

發(fā)布時(shí)間:2022-11-18 09:53:16 來(lái)源:億速云 閱讀:167 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要講解了“vue如何實(shí)現(xiàn)一個(gè)彈窗插件”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“vue如何實(shí)現(xiàn)一個(gè)彈窗插件”吧!

popup.vue

<template>
 <div class="popup-wrapper" v-show="visible" @click="hide">
  <div class="popup-text">{{text}}</div>
 </div>
</template>

組件html結(jié)構(gòu), 外層divposition:fixed定位, 內(nèi)層div顯示彈窗內(nèi)容

export default {
 name: 'popup',
 props: {
  text: { //文字內(nèi)容
   type: String,
   default: ''
  },
  time: { //顯示的時(shí)長(zhǎng)
   type: Number,
   default: 3e3
  },
 },
 data(){
  return {
   visible: false
  }
 },
 methods: {
  open() {
   this.visible = true
   clearTimeout(this.timeout);
   this.$emit('show')
   if(this.time > 0){
    this.timeout = setTimeout(() => {
     this.hide()
    }, this.time)
   }
  },
  hide() {
   this.visible = false
   this.$emit('hide')
   clearTimeout(this.timeout);
  }
 }
}

popup.vue只有2個(gè)屬性: 文本和顯示時(shí)間。組件顯示隱藏由內(nèi)部屬性visible控制,只暴露給外界open和hide2個(gè)方法,2個(gè)方法觸發(fā)對(duì)應(yīng)的事件

測(cè)試一下

<template>
 <popup ref="popup" text="彈窗內(nèi)容" :time="1e3"></popup>
</template>
<script>
import Popup from '@/components/popup'
 ...
  this.$refs.popup.open()
  ...
</script>

插件化

組件功能寫好了,但是這種調(diào)用方式顯得很累贅。舉個(gè)例子layer.js的調(diào)用就是layer.open(...)沒(méi)有import,沒(méi)有ref,當(dāng)然要先全局引用layer。我們寫的彈窗能不能這么方便呢,為此需要把popup改寫成vue插件。
說(shuō)是插件,但能配置屬性調(diào)用方法的還是組件本身,具體是實(shí)例化的組件,而且這個(gè)實(shí)例必須是全局單例,這樣不同vue文件喚起popup的時(shí)候才不會(huì)打架

生成單例

// plugins/popupVm.js
import Popup from '@/components/popup'
let $vm
export const factory = (Vue)=> {
 if (!$vm) {
  let Popup = Vue.extend(PopupComponent)
  $vm = new Popup({
   el: document.createElement('div')
  })
  document.body.appendChild($vm.$el)
 }
 return $vm
}

組件實(shí)例化后是添加在body上的,props不能寫在html里需要js去控制,這里寫個(gè)方法讓屬性默認(rèn)值繼續(xù)發(fā)揮作用

// plugins/util.js
export const setProps = ($vm, options) => {
 const defaults = {}
 Object.keys($vm.$options.props).forEach(k => {
  defaults[k] = $vm.$options.props[k].default
 })
 const _options = _.assign({}, defaults, options)
 for (let i in _options) {
  $vm.$props[i] = _options[i]
 }
}
// plugins/popupPlugin.js
import { factory } from './popupVm'
import { setProps } from './util'

export default {
 install(Vue) {
   let $vm = factory(Vue);

   const popup = {
   open(options) {
    setProps($vm, options)
    //監(jiān)聽(tīng)事件
    typeof options.onShow === 'function' && $vm.$once('show', options.onShow);
    typeof options.onHide === 'function' && $vm.$once('hide', options.onHide);
    $vm.open();
   },
   hide() {
    $vm.hide()
   },
   //只配置文字
   text(text) {
    this.open({ text })
   }
  }
  
  Vue.prototype.$popup = popup
 }
}

在main.js內(nèi)注冊(cè)插件

//main.js
import Vue from 'vue'
import PopupPlugin from '@/plugins/popupPlugin'

Vue.use(PopupPlugin)
在vue框架內(nèi)調(diào)用就非常方便了

<script>
 ...
  this.$popup.text('彈窗消息')
 ...
</script>

Vue的優(yōu)點(diǎn)

Vue具體輕量級(jí)框架、簡(jiǎn)單易學(xué)、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運(yùn)行速度快等優(yōu)勢(shì),Vue中頁(yè)面使用的是局部刷新,不用每次跳轉(zhuǎn)頁(yè)面都要請(qǐng)求所有數(shù)據(jù)和dom,可以大大提升訪問(wèn)速度和用戶體驗(yàn)。

感謝各位的閱讀,以上就是“vue如何實(shí)現(xiàn)一個(gè)彈窗插件”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)vue如何實(shí)現(xiàn)一個(gè)彈窗插件這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI