這篇文章將為大家詳細(xì)講解有關(guān)使用Vue怎么實(shí)現(xiàn)一個(gè)命令式彈窗組件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
公司主營業(yè)務(wù):網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出滄縣免費(fèi)做網(wǎng)站回饋大家。
使用方式:
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注冊組件(使用Vue.use后,會(huì)查找install方法進(jìn)行調(diào)用),將組件調(diào)用方法掛載到Vue原型上。
import Confirm from './components/confirm' Vue.use(Confirm)
關(guān)于使用Vue怎么實(shí)現(xiàn)一個(gè)命令式彈窗組件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章名稱:使用Vue怎么實(shí)現(xiàn)一個(gè)命令式彈窗組件
本文鏈接:http://www.rwnh.cn/article26/jsdpjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、定制開發(fā)、關(guān)鍵詞優(yōu)化、App開發(fā)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)