Element Dialog对话框的使用示例

 更新时间:2020年07月26日 14:29:03   作者:ForeverJPB.  
这篇文章主要介绍了Element 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对话框内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue.js 2.0实现简单分页效果

    vue.js 2.0实现简单分页效果

    这篇文章主要为大家详细介绍了vue.js 2.0实现简单分页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 调用createApp 时Vue工作过程原理

    调用createApp 时Vue工作过程原理

    这篇文章主要为大家介绍了调用createApp 时Vue工作过程原理解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Vue3全局配置Axios并解决跨域请求问题示例详解

    Vue3全局配置Axios并解决跨域请求问题示例详解

    axios 是一个基于promise的HTTP库,支持promise所有的API,本文给大家介绍Vue3全局配置Axios并解决跨域请求问题,内容从axios部署开始到解决跨域问题,感兴趣的朋友一起看看吧
    2023-11-11
  • Vue中的table表单切换实现效果

    Vue中的table表单切换实现效果

    这篇文章主要介绍了Vue中的table表单切换实现效果,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • vue模态框实现动态锚点

    vue模态框实现动态锚点

    这篇文章主要为大家详细介绍了vue模态框实现动态锚点,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • Vue3使用dataV报错问题的解决方法

    Vue3使用dataV报错问题的解决方法

    这篇文章主要为大家详细介绍了Vue3中使用dataV报错问题的解决方法,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • Vue实现自定义组件改变组件背景色(示例代码)

    Vue实现自定义组件改变组件背景色(示例代码)

    要实现 Vue 自定义组件改变组件背景色,你可以通过 props 将背景色作为组件的一个属性传递给组件,在组件内部监听这个属性的变化,并将其应用到组件的样式中,下面通过示例代码介绍Vue如何实现自定义组件改变组件背景色,感兴趣的朋友一起看看吧
    2024-03-03
  • vue项目使用md5加密、crypto-js加密、国密sm3及国密sm4的方法

    vue项目使用md5加密、crypto-js加密、国密sm3及国密sm4的方法

    密码或者其他比较重要东西假如使用明文传输中是很危险的,所以就需要前端一些加密协议,对密码、手机号、身份证号等信息进行保护,下面这篇文章主要给大家介绍了关于vue项目中使用md5加密、crypto-js加密、国密sm3及国密sm4的相关资料,需要的朋友可以参考下
    2022-12-12
  • vue3简单封装input组件和统一表单数据详解

    vue3简单封装input组件和统一表单数据详解

    最近有一个需求是很多个表单添加,编辑等操作,会用到很多input输入框,所以就想把input进行简单封装,这篇文章主要给大家介绍了关于vue3简单封装input组件和统一表单数据的相关资料,需要的朋友可以参考下
    2022-05-05
  • web面试vue自定义组件及调用方式

    web面试vue自定义组件及调用方式

    这篇文章主要介绍了web面试中常问到的关于vue自定义组件及调用方式,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-09-09

最新评论