Modal 对话框
模态对话框,在浮层中显示,引导用户进行相关操作。
提供了两种用法,普通组件使用和封装好的简洁实例调用。
最简单的使用方法,通过控制属性value
来显示 / 隐藏对话框。
可以使用 v-model 实现双向绑定。
默认按键盘ESC
键也可以关闭。
自定义样式
Modal 组件提供了灵活的自定义样式 API 和 Slot,可以自由控制整个 Modal 的各个组成部分,比如页头、页脚、关闭按钮。
通过和其它组件的交互,能实现更复杂的功能,满足了大多业务场景。
<template>
<Button @click="modal2 = true">Custom header and footer</Button>
<Modal v-model="modal2" width="360">
<p slot="header" style="color:#f60;text-align:center">
<Icon type="ios-information-circle"></Icon>
<span>Delete confirmation</span>
</p>
<div style="text-align:center">
<p>After this task is deleted, the downstream 10 tasks will not be implemented.</p>
<p>Will you delete it?</p>
</div>
<div slot="footer">
<Button type="error" size="large" long :loading="modal_loading" @click="del">Delete</Button>
</div>
</Modal>
<Button @click="modal3 = true">No title bar</Button>
<Modal v-model="modal3">
<p>Content of dialog</p>
<p>Content of dialog</p>
<p>Content of dialog</p>
</Modal>
<Button @click="modal4 = true">Internationalization</Button>
<Modal
v-model="modal4"
title="Modal Title"
ok-text="OK"
cancel-text="Cancel">
<p>Something...</p>
<p>Something...</p>
<p>Something...</p>
</Modal>
<Button @click="modal5 = true">Set the width</Button>
<Modal
v-model="modal5"
title="Custom width"
width="300">
<p>Customize width, unit px, default 520px.</p>
<p>The width of the dialog box is responsive, and the width becomes <code>auto</code> when the screen size is less than 768px.</p>
</Modal>
</template>
<script>
export default {
data () {
return {
modal2: false,
modal_loading: false,
modal3: false,
modal4: false,
modal5: false
}
},
methods: {
del () {
this.modal_loading = true;
setTimeout(() => {
this.modal_loading = false;
this.modal2 = false;
this.$Message.success('Successfully delete');
}, 2000);
}
}
}
</script>
给Modal
添加属性loading
后,点击确定按钮对话框不会自动消失,并显示 loading 状态,需要手动关闭对话框,常用于表单提交。
<template>
<Button type="primary" @click="modal6 = true">Display dialog box</Button>
<Modal
v-model="modal6"
title="Title"
:loading="loading"
@on-ok="asyncOK">
<p>After you click ok, the dialog box will close in 2 seconds.</p>
</Modal>
<script>
export default {
data () {
return {
modal6: false,
loading: true
},
methods: {
asyncOK () {
setTimeout(() => {
this.modal6 = false;
}, 2000);
}
}
}
</script>
可以禁用关闭和遮罩层关闭。
自定义位置
可以自定义 Modal 的对话框样式 以及 对话框 Wrap 的 class 名称,从而实现更多自定义的样式,比如垂直居中。
<style lang="less">
.vertical-center-modal{
display: flex;
align-items: center;
justify-content: center;
.ivu-modal{
top: 0;
}
}
</style>
<template>
<Button @click="modal9 = true">20px from the top</Button>
<Modal
title="Title"
v-model="modal9"
:styles="{top: '20px'}">
<p>Content of dialog</p>
<p>Content of dialog</p>
<p>Content of dialog</p>
</Modal>
<Button @click="modal10 = true">Vertical center</Button>
<Modal
title="Title"
v-model="modal10"
class-name="vertical-center-modal">
<p>Content of dialog</p>
<p>Content of dialog</p>
<p>Content of dialog</p>
</Modal>
</template>
<script>
export default {
data () {
return {
modal9: false,
modal10: false,
}
}
}
</script>
设置属性 fullscreen
可以全屏显示。
设置属性 footer-hide
可以隐藏底部内容。
<template>
<Button @click="modal11 = true">Open a fullscreen modal</Button>
<Modal v-model="modal11" fullscreen title="Fullscreen Modal">
<div>This is a fullscreen modal</div>
</Modal>
</template>
<script>
export default {
data () {
return {
modal11: false
}
}
}
</script>
拖拽移动
设置属性 draggable
,对话框可以进行拖拽移动。
实例化使用方法
除了上述通过标准组件的使用方法,iView 还精心封装了一些实例方法,用来创建一次性的轻量级对话框。
四种基本的对话框,只提供一个确定按钮。
<template>
<Button @click="instance('info')">Info</Button>
<Button @click="instance('success')">Success</Button>
<Button @click="instance('warning')">Warning</Button>
<Button @click="instance('error')">Error</Button>
</template>
<script>
export default {
instance (type) {
const title = 'Title';
const content = '<p>Content of dialog</p><p>Content of dialog</p>';
switch (type) {
case 'info':
title: title,
content: content
});
break;
case 'success':
this.$Modal.success({
title: title,
content: content
});
break;
case 'warning':
this.$Modal.warning({
title: title,
content: content
});
break;
case 'error':
this.$Modal.error({
title: title,
content: content
});
break;
}
}
}
}
</script>
确认对话框
快速弹出确认对话框,并且可以自定义按钮文字及异步关闭。
<template>
<Button @click="confirm">Normal</Button>
<Button @click="custom">Custom button text</Button>
<Button @click="async">Asynchronous closing</Button>
</template>
<script>
export default {
methods: {
confirm () {
this.$Modal.confirm({
title: 'Title',
content: '<p>Content of dialog</p><p>Content of dialog</p>',
onOk: () => {
this.$Message.info('Clicked ok');
},
onCancel: () => {
this.$Message.info('Clicked cancel');
}
});
},
custom () {
this.$Modal.confirm({
title: 'Title',
content: '<p>Content of dialog</p><p>Content of dialog</p>',
okText: 'OK',
cancelText: 'Cancel'
});
},
async () {
this.$Modal.confirm({
title: 'Title',
content: '<p>The dialog box will be closed after 2 seconds</p>',
loading: true,
onOk: () => {
setTimeout(() => {
this.$Modal.remove();
this.$Message.info('Asynchronously close the dialog box');
}, 2000);
}
});
}
}
}
</script>
使用 render
字段可以基于 Render 函数来自定义内容。
使用 render 后,将不再限制类型,content 也将无效。
Modal events
Modal instance
通过直接调用以下方法来使用:
this.$Modal.info(config)
this.$Modal.success(config)
this.$Modal.warning(config)
this.$Modal.error(config)
this.$Modal.remove()