vue-dialog的彈出層組件
本文章通過實現(xiàn)一個vue-dialog的彈出層組件,然后附加說明如果發(fā)布此包到npm,且能被其他項目使用。
功能說明
多層彈出時,只有一個背景層。
彈出層嵌入內(nèi)部組件。
彈出層按鈕支持回調(diào)
源碼下載
實現(xiàn)
多層彈出時,只有一個背景層
利用兩個組件實現(xiàn),一個背景層組件(只提供一個背景層,組件名:background.vue),一個彈出層內(nèi)容管理組件(實現(xiàn)多個內(nèi)容層的管理,組件名:master.vue)。
彈出層嵌入內(nèi)部組件
使用vue的component組件實現(xiàn),他可以完美支持。
彈出層按鈕支持回調(diào)
在master.vue中實現(xiàn),詳細解析此代碼
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:一個computed屬性,讀取props的mIndex屬性,表示內(nèi)部層的zIndex層級關(guān)系。
component加載組件
btns:表示按鈕的集合,現(xiàn)還不支持組件獨立配置按鈕列表。
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方法,用于打開彈出層,且返回一個Promise。
嵌入background.vue組件,用于提供背景層。
clickHandler方法:master.vue組件按鈕的事件響應(yīng)函數(shù),會resolve在open方法中提供的promise。
css代碼:
<style> .modal-content { position: absolute; } </style>
如何實用
首先需要在頂層引入master.vue,然后嵌入到與app組件平級,如下代碼:
new Vue({ el: '#app', template: '<div><App></App><HDialog ref="hDialog" ></HDialog></div>', components: { App } })
一定要指定ref值,這是彈出層實現(xiàn)的關(guān)鍵。
在任意一個子組件中如下使用:
let promise = this.$root.$refs.hDialog.open({ template: '<div>第二層了</div>' }) promise.then(function (arg) { alert('第二層' + arg.type) arg.close() })
使用$root.$refs找到彈出層管理組件
使用調(diào)用其open方法,并接受一個promise類型的返回值
使用promise即可。
發(fā)布到npm
如果組件需要被其他人引用,最好使用commonjs2規(guī)范,webapck如下配置:
output: { path: './dist', filename: '[name].js', library: 'vue-hdialog', libraryTarget: 'commonjs2' }
- 在npmjs上注冊一個賬號
- 利用npm login 登錄
- 使用npm publish 發(fā)布,如果你想卸載可以用npm unpublish --force.
- 發(fā)布是需要package.json檢測version和name字段,如果已存,或者是存在被卸載的都不行。
- package.json中的main節(jié)點是指定其他引用時,默認(rèn)導(dǎo)出的文件。
本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(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的使用案例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法
這篇文章主要給大家介紹了關(guān)于vue項目打包發(fā)布到Nginx后無法訪問后端接口的解決辦法,記錄一下項目需要注意的地方,方便以后快速使用,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-04-04