封装优点
项目开发中el-dialog弹窗的使用非常高频,每次使用都需要监听关闭与打开重复造轮子,非常之麻烦!!
所以启动CV大法!!
在components中新建一个DiaWindow.vue文件
<template>
<div>
<el-dialog
:title="titleName"
:visible.sync="diaVisible"
:width="width"
:append-to-body="appendToBody"
:before-close="handleClose" v-bind="$attrs" v-on="$listeners">
<slot name="diaContent"></slot>
</el-dialog>
</div>
</template>
<script>
export default {
props: {
titleName: {
type: String,
default: '标题',
},
diaVisible: {
type: Boolean,
default: false,
},
width: {
type: String,
default: '80%',
},
appendToBody: {
type: Boolean,
default: true,
},
},
data() {
return {
}
},
mounted() {
},
methods: {
handleClose() {
this.$emit('update:diaVisible', false)
},
},
}
</script>
<style lang="scss" scoped></style>
组件使用
<template>
<div>
<DiaWindow
:titleName="'标题'"
:diaVisible.sync="diaView"
v-bind="$attrs"
v-on="$listeners">
<template>
<div slot="diaContent">
<-- 定义内容写在这里 -->
</div>
</template>
</DiaWindow>
</div>
</template>
<script>
import DiaWindow from '@/components/DiaWindow.vue';
export default {
components: { DiaWindow, },
data() {
return {
diaView: false,
}
},
mounted() {
},
methods: {
},
}
</script>
<style lang="scss" scoped></style>