vue-dialog的彈出層組件
本文章通過實(shí)現(xiàn)一個(gè)vue-dialog的彈出層組件,然后附加說明如果發(fā)布此包到npm,且能被其他項(xiàng)目使用。
功能說明
多層彈出時(shí),只有一個(gè)背景層。
彈出層嵌入內(nèi)部組件。
彈出層按鈕支持回調(diào)
源碼下載
實(shí)現(xiàn)
多層彈出時(shí),只有一個(gè)背景層
利用兩個(gè)組件實(shí)現(xiàn),一個(gè)背景層組件(只提供一個(gè)背景層,組件名:background.vue),一個(gè)彈出層內(nèi)容管理組件(實(shí)現(xiàn)多個(gè)內(nèi)容層的管理,組件名:master.vue)。
彈出層嵌入內(nèi)部組件
使用vue的component組件實(shí)現(xiàn),他可以完美支持。
彈出層按鈕支持回調(diào)
在master.vue中實(shí)現(xiàn),詳細(xì)解析此代碼
html代碼
<template> <div> <div class="modal-content" v-for="(comp,index) in comps" v-bind:style="style(index,comp)" > <div class="modal-header" > header </div> <div class="modal-body"> <component :is="comp"></component> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" v-on:click="clickHandler(btn.value, comp, index)" v-for="btn in btns" >{{btn.text}}</button> </div> </div> <hDialogBack ref="back" v-bind:z-index="realIndex-1" ></hDialogBack> </div> </template>
comps:內(nèi)部組件的集合
realIndex:一個(gè)computed屬性,讀取props的mIndex屬性,表示內(nèi)部層的zIndex層級關(guān)系。
component加載組件
btns:表示按鈕的集合,現(xiàn)還不支持組件獨(dú)立配置按鈕列表。
style:此方法用于生成內(nèi)部組件居中的css代碼。
js代碼:
<script> import hDialogBack from './background' function getclientPoint () { return { width: document.documentElement.clientWidth || document.body.clientWidth, height: document.documentElement.clientHeight || document.body.clientHeight } } export default { name: 'HDialog', data () { return { comps: [] } }, props: { 'btns': { type: Array, default: function () { return [{ text: 'ok', value: 'ok'}, { text: 'cancel', value: 'cancel'}] } }, 'mIndex': { type: Number, default: 19861016 } }, computed: { realIndex: function () { return this.mIndex } }, components: { hDialogBack }, methods: { open: function (comp) { comp.promise = new Promise(function (resolve, reject) { comp.resolve = resolve comp.reject = reject }) comp.width = comp.width || 600 comp.height = comp.height || 400 this.comps.push(comp) if (!this.$refs.back.show) { this.$refs.back.open() } return comp.promise }, clickHandler: function (type, comp, index) { let self = this let close = function () { self.comps.splice(index, 1) if (self.comps.length === 0 && self.$refs.back.show) { self.$refs.back.close() } } /** 只提供promise模式 */ comp.resolve({'type': type, 'close': close}) }, style: function (index, comp) { let point = getclientPoint() return { zIndex: this.realIndex + index, width: comp.width + 'px', height: comp.height + 'px', left: ((point.width - comp.width) / 2) + 'px', top: ((point.height - comp.height) / 2) + 'px' } } } } </script>
open方法,用于打開彈出層,且返回一個(gè)Promise。
嵌入background.vue組件,用于提供背景層。
clickHandler方法:master.vue組件按鈕的事件響應(yīng)函數(shù),會resolve在open方法中提供的promise。
css代碼:
<style> .modal-content { position: absolute; } </style>
如何實(shí)用
首先需要在頂層引入master.vue,然后嵌入到與app組件平級,如下代碼:
new Vue({ el: '#app', template: '<div><App></App><HDialog ref="hDialog" ></HDialog></div>', components: { App } })
一定要指定ref值,這是彈出層實(shí)現(xiàn)的關(guān)鍵。
在任意一個(gè)子組件中如下使用:
let promise = this.$root.$refs.hDialog.open({ template: '<div>第二層了</div>' }) promise.then(function (arg) { alert('第二層' + arg.type) arg.close() })
使用$root.$refs找到彈出層管理組件
使用調(diào)用其open方法,并接受一個(gè)promise類型的返回值
使用promise即可。
發(fā)布到npm
如果組件需要被其他人引用,最好使用commonjs2規(guī)范,webapck如下配置:
output: { path: './dist', filename: '[name].js', library: 'vue-hdialog', libraryTarget: 'commonjs2' }
- 在npmjs上注冊一個(gè)賬號
- 利用npm login 登錄
- 使用npm publish 發(fā)布,如果你想卸載可以用npm unpublish --force.
- 發(fā)布是需要package.json檢測version和name字段,如果已存,或者是存在被卸載的都不行。
- package.json中的main節(jié)點(diǎn)是指定其他引用時(shí),默認(rèn)導(dǎo)出的文件。
本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue修改iview組件的樣式的兩種方案(element同)
使用vue必然會用到等iview組件庫,但是iview的組件的樣式跟自己寫的div的樣式修改不太一樣,所以本文給大家介紹了Vue修改iview組件的樣式的兩種方案(element同),需要的朋友可以參考下2024-04-04electron?dialog.showMessageBox的使用案例
Electron?Dialog?模塊提供了api來展示原生的系統(tǒng)對話框,本文主要介紹了electron?dialog.showMessageBox的使用案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Vue3+ElementPlus封裝圖片空間組件的門面實(shí)例
圖片空間是用于管理上傳圖片的工具,可以讓用戶方便地存儲、管理和調(diào)用圖片,提高工作效率,它通常具備多樣的樣式,但操作入口統(tǒng)一,便于使用,通過圖片空間組件,用戶能直接在其他模塊(例如商品圖片)中選擇所需圖片2024-09-09vue項(xiàng)目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法,記錄一下項(xiàng)目需要注意的地方,方便以后快速使用,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04Vue3+Vite項(xiàng)目使用less的實(shí)現(xiàn)步驟
最近學(xué)習(xí)在vite項(xiàng)目中配置less,本文主要介紹了Vue3+Vite項(xiàng)目使用less的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02