vue-dialog的弹出层组件

2019-11-22,,,

本文章通过实现一个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节点是指定其他引用时,默认导出的文件。

本文已被整理到了《Vue.js前端组件学习教程》,欢迎大家学习阅读。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

更多vue学习教程请阅读专题《vue实战教程》

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持北冥有鱼。

您可能感兴趣的文章:

  • vue如何引用其他组件(css和js)
  • Vuejs第九篇之组件作用域及props数据传递实例详解
  • 详解vue2父组件传递props异步数据到子组件的问题
  • Vue2实现组件props双向绑定
  • vue之父子组件间通信实例讲解(props、$ref、$emit)
  • 详解vue2.0 使用动态组件实现 Tab 标签页切换效果(vue-cli)
  • Vue-Router来实现组件间跳转的三种方法
  • vue mounted组件的使用
  • vue组件 $children,$refs,$parent的使用详解
  • 浅入深出Vue之组件使用

《vue-dialog的弹出层组件.doc》

下载本文的Word格式文档,以方便收藏与打印。