vue element实现弹出窗口
使用 Vue 和 Element UI 实现弹出窗口
Element UI 提供了 el-dialog 组件用于实现弹出窗口功能,以下是一个完整的实现示例:
基本弹窗实现
安装 Element UI(如果尚未安装):
npm install element-ui -S
在 Vue 项目中注册 Element UI:
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
模板部分:
<template>
<div>
<el-button type="primary" @click="dialogVisible = true">打开弹窗</el-button>
<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%">
<span>这是一段内容</span>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="dialogVisible = false">确定</el-button>
</span>
</el-dialog>
</div>
</template>
脚本部分:
<script>
export default {
data() {
return {
dialogVisible: false
}
}
}
</script>
自定义弹窗内容
弹窗内容可以完全自定义:
<el-dialog
title="自定义内容"
:visible.sync="dialogVisible"
width="50%">
<el-form :model="form">
<el-form-item label="姓名">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="年龄">
<el-input v-model="form.age"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">提交</el-button>
</span>
</el-dialog>
对应的数据和方法:
data() {
return {
dialogVisible: false,
form: {
name: '',
age: ''
}
}
},
methods: {
handleSubmit() {
console.log(this.form)
this.dialogVisible = false
}
}
弹窗属性配置
el-dialog 支持多种配置选项:
<el-dialog
title="可配置弹窗"
:visible.sync="dialogVisible"
width="60%"
:close-on-click-modal="false"
:show-close="false"
:before-close="handleClose">
内容区域
</el-dialog>
对应的关闭处理方法:
methods: {
handleClose(done) {
this.$confirm('确认关闭?')
.then(_ => {
done()
})
.catch(_ => {})
}
}
全屏弹窗
实现全屏弹窗:
<el-dialog
title="全屏弹窗"
:visible.sync="dialogVisible"
fullscreen>
全屏内容
</el-dialog>
嵌套弹窗
支持弹窗内再打开弹窗:
<el-dialog
title="外层弹窗"
:visible.sync="outerVisible"
width="50%">
<el-button @click="innerVisible = true">打开内层弹窗</el-button>
<el-dialog
title="内层弹窗"
:visible.sync="innerVisible"
append-to-body
width="30%">
内层内容
</el-dialog>
</el-dialog>
对应的数据:
data() {
return {
outerVisible: false,
innerVisible: false
}
}
以上示例展示了 Vue 和 Element UI 实现弹窗的基本方法和进阶用法,可以根据实际需求选择合适的实现方式。







