ElementUI对话框(dialog)提取为子组件
<template>
<div class="main-wrap">
<el-button type="primary" @click="add('addOrder')">添加</el-button>
<add-order ref="addOrder" v-if="addOrderVisible" :visible.sync="addOrderVisible"></add-order>
</div>
</template>
<script>
import Add from './add.vue'
export default {
data(){
return {
addOrderVisible: false
}
},
methods: {
add(refForm){
if(this.$refs[refForm]){
this.$refs[refForm].initForm();
}
this.addOrderVisible= true;
}
},
components: {
'add-order': Add
}
}
</script>
<template>
<el-dialog :visible.sync="visible" :show-close="false" width="600px" :modal="true" :close-on-click-modal="false" :close-on-press-escape="false">
<h2 slot="title">添加订单</h2>
<button type="button" aria-label="Close" class="el-dialog__headerbtn" @click.stop="cancelModal"><i class="el-dialog__close el-icon el-icon-close"></i></button>
<el-form class="form-wrapper" ref="orderForm" :model="orderForm" :rules="addRules" label-width="110px">
<el-form-item label="联系人:" prop="fromContact">
<el-input v-model="orderForm.fromContact" type="text" placeholder="请输入联系人名称"></el-input>
</el-form-item>
<el-form-item label="联系电话:" prop="fromPhone">
<el-input v-model="orderForm.fromPhone" type="text" placeholder="请输入联系电话"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="buttons-wrap">
<el-button type="primary">确定</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
props: {
visible: {
type: Boolean,
default: false
}
},
data(){
return {
orderForm: {},
addRules: {
fromContact: [{ required: true, message: "请输入联系人姓名", trigger: 'blur'}],
fromPhone: [{required: true, message: "请输入", trigger: 'blur'}]
}
}
},
methods: {
initForm(){
this.orderForm = {
fromContact: '',
fromPhone: ''
};
if(this.$refs.orderForm){
this.$refs.orderForm.resetFields();
}
},
cancelModal(){
// 关闭弹窗,触发父组件修改visible值
this.$emit('update:visible', false);
}
}
}
</script>
<style lang="scss" scoped>
.buttons-wrap { .el-button {
margin-right: 20px;
min-width: 100px;
}
}
</style>
父组件:
<template>
<div class="main-wrap">
<el-button type="primary" @click="toAdd">添加</el-button>
<add-order ref="orderAdd" v-if="addOrderVisible" :visible.sync="addOrderVisible"></add-order>
</div>
</template>
<script>
import Add from './add.vue'
export default {
data(){
return {
addOrderVisible: false
}
},
methods: {
toAdd() {
this.addOrderVisible = true;
}
},
components: {
'add-order': Add
}
}
</script>
子组件:
<template>
<el-dialog
title="添加"
v-loading="loading"
:visible.sync="visible"
width="600px"
:before-close="modalClose"
:close-on-click-modal="false"
:close-on-press-escape="false">
<div>弹窗内容</div>
</el-dialog>
</template>
<script>
export default {
data() {
return {
loading: false
}
},
props: {
visible: {
type: Boolean,
default: false
}
},
methods: {
modalClose() {
this.$emit('update:visible', false); // 直接修改父组件的属性
}
}
}
</script>
这里提另外一种情况:
<template>
<div class="main-wrap">
<el-button type="primary" @click="showDetail()">详情</el-button>
<el-dialog v-if="detailVisible" :visible.sync="detailVisible" width="600px" :modal="true">
<h2 slot="title">详情</h2>
<detail ref="newOrderDetail" :newOrderDetail="newOrderDetail"></detail>
<div slot="footer" class="detail-wrap-bottom">
<el-button type="primary">确认</el-button>
<el-button type="default">退回</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import detail from './detail'
export default {
data(){
return {
detailVisible: false,
newOrderDetail: {}
}
},
methods: {
showDetail(){
this.newOrderDetail = {id: 8, fromContact: 'Thomas', fromPhone: '15812345678'};
this.detailVisible= !this.detailVisible;
}
},
components: {
detail: detail
}
}
</script>
<template>
<div class="detail-wrap" :data="newOrderDetail">
<ul>
<li><span>用车联系人:</span><div>{{newOrderDetail.fromContact}}</div></li>
<li><span>联系人电话:</span><div>{{newOrderDetail.fromPhone}}</div></li>
</ul>
</div>
</template>
<script>
export default {
props: {
newOrderDetail: {
type: Object,
default: null
}
},
data() {
return {
loading: false
}
}
}
</script>
<style lang="scss" scoped>
.detail-wrap ul {
max-height: 400px;
overflow: auto; li {
position: relative;
padding: 8px;
font-size: 1rem; span {
position: absolute;
width: 90px;
} & > div {
margin-left: 90px;
}
}
}
</style>
温馨提示:
如果弹窗内容较多,出现了滚动条,需要每次打开还原到顶部,则需要添加v-if指令,因为这个指令是动态渲染内容的(文中有用到)。
ElementUI对话框(dialog)提取为子组件的更多相关文章
- elementUI 的el-dialog作为子组件,父组件如何控制其关闭的按钮
这里有三点需要说明: 1. 使用:before-close="closeHandle" 将其 $emit() 出去 2. 取消按钮 也需要$emeit出去 3. 控制对话框显示隐藏 ...
- vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值。
vue+elementUI项目,父组件向子组件传值,子组件向父组件传值,父子组件互相传值. vue 父组件与子组件相互通信 一.父组件给子组件传值 props 实现父组件向子组件传值. 1父组件里: ...
- java 图形化小工具Abstract Window Toolit 常用组件:对话框Dialog FileDialog
对话框 Dialog是Window类的子类,是1个容器类,属于特殊组件,对话框是可以独立存在的顶级窗口,因此用法与普通窗口的用法几乎完全一样.但对话框有如下两点需要注意. (1),对话框通常依赖于其他 ...
- element-ui(或者说Vue的子组件)绑定的方法中传入自定义参数
比如el-upload中的 :on-success= fn,其实是给组件el-upload传递一个prop,这样写的话fn只能接受upload组件规定的参数,如果想自己传递父组件中的参数比如b,要写成 ...
- 对话框Dialog
QMainWindow QMainWindow是 Qt 框架带来的一个预定义好的主窗口类. 主窗口,就是一个普通意义上的应用程序(不是指游戏之类的那种)最顶层的窗口.通常是由一个标题栏,一个菜单栏,若 ...
- vue实现element-ui对话框可拖拽功能
element-ui对话框可拖拽及边界处理 应业务需求,需要实现对话框可拖拽问题,应element-ui没有提供官方支持,于是便参考大神的文章,得出了适合业务需要的解决方案.很多大神给出的代码是没有解 ...
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
- vue父组件向子组件传对象,不实时更新解决
vue报错:void mutating a prop directly since the value will be overwritten whenever the parent componen ...
- 实现element-ui对话框可拖拽功能
element-ui对话框可拖拽及边界处理 应业务需求,需要实现对话框可拖拽问题,应element-ui没有提供官方支持,于是便参考大神的文章,得出了适合业务需要的解决方案.很多大神给出的代码是没有解 ...
随机推荐
- PHP 循环输出多重数组元素
<?php $arr = array( array( array( array( array( ,, ) ) ), array( ,,) ) ), array(, , ) ); function ...
- ImageSwitcher 图片切换器
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- osg Error osgearth_viewerd fails with "Loaded scene graph does not contain a MapNode
void StateSet::setGlobalDefaults() ShaderPipeline disabled.void StateSet::setGlobalDefaults() Shader ...
- Pythonrandom模块(获取随机数)常用方法和使用例子
Python random模块(获取随机数)常用方法和使用例子 这篇文章主要介绍了Python random模块(获取随机数)常用方法和使用例子,需要的朋友可以参考下 random.random ra ...
- python 求交集、并集、差集
需要用到set类型 交集,两种方法 retA = [i for i in listA if i in listB] retB = setA.intersection(setB) 并集 retC = s ...
- LODOP粒度TableRowThickNess合并行测试
之前的博文:LODOP打印table不切行TableRowThickNess. ,中是没有合并行等的表格,通过设置增大分页粒度,会找附近的表格线,然后根据表格线分页,避免了切行.如果有比较复杂的表格, ...
- 高级UI-自定义Behavior
Behavior本身是一个抽象类,可以用于两个view之间的状态监听,也可用于某个view监听CoordinateLayout里面的所有控件滑动状态,实现自定义Behavior则可以实现任意两个vie ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
- eclipse设置text file encoding UTF-8和文件的换行符 Unix 格式
阿里华山版java开发手册代码格式第10条: 步骤:1.Window - Preferences, 2.左边选择 General - Workspace , 3.右边Text file encodin ...
- 机器学习-LDA主题模型笔记
LDA常见的应用方向: 信息提取和搜索(语义分析):文档分类/聚类.文章摘要.社区挖掘:基于内容的图像聚类.目标识别(以及其他计算机视觉应用):生物信息数据的应用; 对于朴素贝叶斯模型来说,可以胜任许 ...