Element Dialog对话框的使用示例
程序员文章站
2022-03-27 19:32:36
组件— 对话框基本用法点击打开 dialog &...
组件— 对话框
基本用法
<el-button type="text" @click="dialogvisible = true">点击打开 dialog</el-button> <el-dialog title="提示" :visible.sync="dialogvisible" width="30%" :before-close="handleclose"> <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> <script> export default { data() { return { dialogvisible: false }; }, methods: { handleclose(done) { this.$confirm('确认关闭?') .then(_ => { done(); }) .catch(_ => {}); } } }; </script>
自定义内容
<!-- table --> <el-button type="text" @click="dialogtablevisible = true">打开嵌套表格的 dialog</el-button> <el-dialog title="收货地址" :visible.sync="dialogtablevisible"> <el-table :data="griddata"> <el-table-column property="date" label="日期" width="150"></el-table-column> <el-table-column property="name" label="姓名" width="200"></el-table-column> <el-table-column property="address" label="地址"></el-table-column> </el-table> </el-dialog> <!-- form --> <el-button type="text" @click="dialogformvisible = true">打开嵌套表单的 dialog</el-button> <el-dialog title="收货地址" :visible.sync="dialogformvisible"> <el-form :model="form"> <el-form-item label="活动名称" :label-width="formlabelwidth"> <el-input v-model="form.name" autocomplete="off"></el-input> </el-form-item> <el-form-item label="活动区域" :label-width="formlabelwidth"> <el-select v-model="form.region" placeholder="请选择活动区域"> <el-option label="区域一" value="shanghai"></el-option> <el-option label="区域二" value="beijing"></el-option> </el-select> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogformvisible = false">取 消</el-button> <el-button type="primary" @click="dialogformvisible = false">确 定</el-button> </div> </el-dialog> <script> export default { data() { return { griddata: [{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }], dialogtablevisible: false, dialogformvisible: false, form: { name: '', region: '', date1: '', date2: '', delivery: false, type: [], resource: '', desc: '' }, formlabelwidth: '120px' }; } }; </script>
嵌套的 dialog
<template> <el-button type="text" @click="outervisible = true">点击打开外层 dialog</el-button> <el-dialog title="外层 dialog" :visible.sync="outervisible"> <el-dialog width="30%" title="内层 dialog" :visible.sync="innervisible" append-to-body> </el-dialog> <div slot="footer" class="dialog-footer"> <el-button @click="outervisible = false">取 消</el-button> <el-button type="primary" @click="innervisible = true">打开内层 dialog</el-button> </div> </el-dialog> </template> <script> export default { data() { return { outervisible: false, innervisible: false }; } } </script>
居中布局
<template> <el-button type="text" @click="outervisible = true">点击打开外层 dialog</el-button> <el-dialog title="外层 dialog" :visible.sync="outervisible"> <el-dialog width="30%" title="内层 dialog" :visible.sync="innervisible" append-to-body> </el-dialog> <div slot="footer" class="dialog-footer"> <el-button @click="outervisible = false">取 消</el-button> <el-button type="primary" @click="innervisible = true">打开内层 dialog</el-button> </div> </el-dialog> </template> <script> export default { data() { return { outervisible: false, innervisible: false }; } } </script>
attributes
slot
events
element 对话框简单使用
官方文档介绍的是页内对话框,但没有基于组件的对话框,这里记录一下,原理就是父子传值是否显示
父页导入组件
<template> <div class="home"> <el-button @click="btnadd">添加用户</el-button> <dialog :visible.sync="visible" title="添加用户"></dialog> </div> </template> <script> import dialog from "../components/dialog"; export default { name: 'home', components: { dialog }, data() { return { visible: false } }, methods: { btnadd() { this.visible = true } } } </script>
对话框
<template> <div> <el-dialog v-bind="$attrs" v-on="$listeners" @open="onopen" @close="onclose" :title="title" 对话框打开后是否可以点击后边灰色区域关闭对话框 :close-on-click-modal='false'> <el-row :gutter="15"> <el-form ref="elform" :model="formdata" :rules="rules" size="medium" label-width="100px"> <el-col :span="23"> <el-form-item label="姓名" prop="username"> <el-input v-model="formdata.username" placeholder="请输入姓名" :maxlength="50" clearable prefix-icon='el-icon-user-solid' :style="{width: '100%'}"></el-input> </el-form-item> </el-col> <el-col :span="23"> <el-form-item label="性别" prop="sex"> <el-radio-group v-model="formdata.sex" size="medium"> <el-radio v-for="(item, index) in sexoptions" :key="index" :label="item.value" :disabled="item.disabled">{{item.label}} </el-radio> </el-radio-group> </el-form-item> </el-col> <el-col :span="23"> <el-form-item label="生日" prop="birthday"> <el-date-picker v-model="formdata.birthday" format="yyyy-mm-dd" value-format="yyyy-mm-dd" :style="{width: '100%'}" placeholder="请选择生日" clearable></el-date-picker> </el-form-item> </el-col> </el-form> </el-row> <div slot="footer"> <el-button @click="close">取消</el-button> <el-button type="primary" @click="handelconfirm">确定</el-button> </div> </el-dialog> </div> </template>
<script> export default { inheritattrs: false, props: { title: string }, data() { return { formdata: { username: undefined, sex: 3, birthday: null, }, rules: { username: [{ required: true, message: '请输入姓名', trigger: 'blur' }], sex: [{ required: true, message: '性别不能为空', trigger: 'change' }], birthday: [{ required: true, message: '请选择生日', trigger: 'change' }], }, sexoptions: [{ "label": "男", "value": 1 }, { "label": "女", "value": 2 }, { "label": "保密", "value": 3 }], } }, methods: { onopen() { //打开对话框执行 }, onclose() { //关闭对话框执行清除以上字段内容 this.$refs['elform'].resetfields() }, close() { //手工调用关闭,显示状态为隐藏 this.$emit('update:visible', false) }, handelconfirm() { this.$refs['elform'].validate(valid => { if (valid) { //点击确定后执行验证并执行方法,执行完毕后调用close()方法 this.$message.success({ message: "成功了" }) this.close() } }) } } } </script>
执行效果
到此这篇关于element dialog对话框的使用示例的文章就介绍到这了,更多相关element dialog对话框内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读