本文章通過實現一個vue-dialog的彈出層組件,然后附加說明如果發布此包到npm,且能被其他項目使用。
功能說明
- 多層彈出時,只有一個背景層。
- 彈出層嵌入內部組件。
- 彈出層按鈕支持回調
- 源碼下載
實現
- 多層彈出時,只有一個背景層
利用兩個組件實現,一個背景層組件(只提供一個背景層,組件名:background.vue),一個彈出層內容管理組件(實現多個內容層的管理,組件名:master.vue)。 - 彈出層嵌入內部組件
使用vue的component組件實現,他可以完美支持。 - 彈出層按鈕支持回調
在master.vue中實現,詳細解析此代碼
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:內部組件的集合
- realIndex:一個computed屬性,讀取props的mIndex屬性,表示內部層的zIndex層級關系。
- component加載組件
- btns:表示按鈕的集合,現還不支持組件獨立配置按鈕列表。
- style:此方法用於生成內部組件居中的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組件按鈕的事件響應函數,會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值,這是彈出層實現的關鍵。
- 在任意一個子組件中如下使用:
let promise = this.$root.$refs.hDialog.open({ template: '<div>第二層了</div>' }) promise.then(function (arg) { alert('第二層' + arg.type) arg.close() })
- 使用$root.$refs找到彈出層管理組件
- 使用調用其open方法,並接受一個promise類型的返回值
- 使用promise即可。
發布到npm
- 如果組件需要被其他人引用,最好使用commonjs2規范,webapck如下配置:
output: { path: './dist', filename: '[name].js', library: 'vue-hdialog', libraryTarget: 'commonjs2' }
- 在npmjs上注冊一個賬號
- 利用npm login 登錄
- 使用npm publish 發布,如果你想卸載可以用npm unpublish --force.
- 發布是需要package.json檢測version和name字段,如果已存,或者是存在被卸載的都不行。
- package.json中的main節點是指定其他引用時,默認導出的文件。