Vue 實(shí)現(xiàn)一個(gè)命令式彈窗組件功能
前言
在日常工作中彈窗組件是很常用的組件,但用得多還是別人的,空閑時(shí)間就自己來簡(jiǎn)單實(shí)現(xiàn)一個(gè)彈窗組件
涉及知識(shí)點(diǎn):extend、$mount、$el
使用方式:
this.$Confirm({
title:'自定義標(biāo)題'
}).then(res=>{
console.log(res)
})
目錄結(jié)構(gòu)

index.vue:組件布局、樣式、交互邏輯
index.js:掛載組件、暴露方法
知識(shí)點(diǎn)
在此之前,了解下涉及的知識(shí)點(diǎn)
1. extend

使用這個(gè)api,可以將引入的vue組件變成vue構(gòu)造函數(shù),實(shí)例化后方便進(jìn)行擴(kuò)展
2. $mount

我們希望彈窗組件是在使用時(shí)才顯示出來,那么就需要?jiǎng)討B(tài)的向body中添加元素。使用$mount方法可以手動(dòng)掛載一個(gè)vue實(shí)例,和 extend 剛好搭配使用,這個(gè)也是彈窗組件命令式的關(guān)鍵。
3. $el

既然要添加dom元素,通過實(shí)例的$el屬性,正好可以取到dom元素,之后就是使用原生方法進(jìn)行添加節(jié)點(diǎn)啦~
代碼實(shí)現(xiàn)
index.vue
<template>
<div class="wrap">
<div class="main">
<div class="content">
{{title}}
</div>
<div class="btn-grounp">
<div class="btn cancel" @click="cancel">{{cancelText}}</div>
<div class="btn confirm" @click="confirm">{{confirmText}}</div>
</div>
</div>
</div>
</template>
<script>
export default {
name:'',
data () {
return {
title:'這是一個(gè)彈窗',
confirmText:'確定',
cancelText:'取消'
};
},
methods: {
show(cb){
typeof cb === 'function' && cb.call(this,this)
return new Promise(resolve=>{
this.resolve = resolve
})
},
confirm(){
this.resolve('confirm')
this.hide()
},
cancel(){
this.resolve('cancel')
this.hide()
},
hide(){
document.body.removeChild(this.$el)
this.$destroy()
}
},
}
</script>
<style scoped>
.wrap{
position: fixed;
top: 0;
bottom:0;
left:0;
right:0;
display:flex;
justify-content: center;
align-items: center;
background: rgba(0,0,0,.3);
}
.main{
width: 30%;
padding: 10px;
background: #fff;
box-shadow: 0 0 10px 1px #ddd;
border-radius: 5px;
}
.content{
color:#424242;
font-size: 20px;
}
.btn-grounp{
margin-top: 15px;
display:flex;
justify-content: flex-end;
}
.btn{
margin-left: 15px;
padding: 5px 20px;
border-radius: 5px;
font-size: 16px;
color:#fff;
}
.confirm{
background: lightblue;
}
.cancel{
background: lightcoral;
}
</style>
index.js
import Vue from 'vue'
import comfirm from './index.vue'
let newInstance = null
//將vue組件變?yōu)闃?gòu)造函數(shù)
let ConfirmConstructor = Vue.extend(comfirm)
let init = (options)=>{
//實(shí)例化組件
newInstance = new ConfirmConstructor()
//合并配置選項(xiàng)
Object.assign(newInstance,options)
//加載dom
document.body.appendChild(newInstance.$el)
}
let caller = (options)=>{
//options 為調(diào)用組件方法時(shí)傳入的配置選項(xiàng)
if(!newInstance){
init(options)
}
return newInstance.show(vm =>{newInstance = null})
}
export default {
install(vue){
vue.prototype.$Confirm = caller
}
}
main.js
上面我對(duì)外暴露的對(duì)象中含有install方法,這里可以使用Vue.use注冊(cè)組件(使用Vue.use后,會(huì)查找install方法進(jìn)行調(diào)用),將組件調(diào)用方法掛載到Vue原型上。
import Confirm from './components/confirm' Vue.use(Confirm)
寫在最后
這個(gè)彈窗組件比較簡(jiǎn)陋,還有很多地方可以完善,等有時(shí)間再搞了~
總結(jié)
以上所述是小編給大家介紹的Vue 實(shí)現(xiàn)一個(gè)命令式彈窗組件功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列
這篇文章主要為大家詳細(xì)介紹了vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
vue中g(shù)etters的使用與四個(gè)map方法的使用方式
這篇文章主要介紹了vue中g(shù)etters的使用與四個(gè)map方法的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
vue父組件監(jiān)聽子組件數(shù)據(jù)更新方式(hook)
這篇文章主要介紹了vue父組件監(jiān)聽子組件數(shù)據(jù)更新方式(hook),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue實(shí)戰(zhàn)之vue登錄驗(yàn)證的實(shí)現(xiàn)代碼
本篇文章主要介紹了Vue實(shí)戰(zhàn)之vue登錄的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Vue中Axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求的方法
為了防止用戶在網(wǎng)絡(luò)不好或者其他情況下短時(shí)間內(nèi)重復(fù)進(jìn)行接口請(qǐng)求,重復(fù)發(fā)送多次請(qǐng)求,本文主要介紹了Vue中Axios中取消請(qǐng)求及阻止重復(fù)請(qǐng)求的方法,感興趣的可以了解一下2022-02-02
Vue監(jiān)聽一個(gè)數(shù)組id是否與另一個(gè)數(shù)組id相同的方法
今天小編就為大家分享一篇Vue監(jiān)聽一個(gè)數(shù)組id是否與另一個(gè)數(shù)組id相同的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue3如何利用xlsx、xlsx-js-style導(dǎo)出Excel表格使用(適合新手)
在Vue項(xiàng)目中導(dǎo)出Excel表格是常見的功能,特別是在后臺(tái)管理系統(tǒng)中,為了方便用戶將大量數(shù)據(jù)保存為本地文件,這篇文章主要給大家介紹了關(guān)于Vue3如何利用xlsx、xlsx-js-style導(dǎo)出Excel表格使用的相關(guān)資料,需要的朋友可以參考下2024-06-06

