Modal 对话框

    模态对话框,在浮层中显示,引导用户进行相关操作。

    提供了两种用法,普通组件使用和封装好的简洁实例调用。

    基础用法

    最简单的使用方法,通过控制属性value来显示 / 隐藏对话框。

    可以使用 v-model 实现双向绑定。

    默认按键盘ESC键也可以关闭。

    Modal 对话框 - 图2

    自定义样式

    Modal 组件提供了灵活的自定义样式 API 和 Slot,可以自由控制整个 Modal 的各个组成部分,比如页头、页脚、关闭按钮。

    通过和其它组件的交互,能实现更复杂的功能,满足了大多业务场景。

    1. <template>
    2. <Button @click="modal2 = true">Custom header and footer</Button>
    3. <Modal v-model="modal2" width="360">
    4. <p slot="header" style="color:#f60;text-align:center">
    5. <Icon type="ios-information-circle"></Icon>
    6. <span>Delete confirmation</span>
    7. </p>
    8. <div style="text-align:center">
    9. <p>After this task is deleted, the downstream 10 tasks will not be implemented.</p>
    10. <p>Will you delete it?</p>
    11. </div>
    12. <div slot="footer">
    13. <Button type="error" size="large" long :loading="modal_loading" @click="del">Delete</Button>
    14. </div>
    15. </Modal>
    16. <Button @click="modal3 = true">No title bar</Button>
    17. <Modal v-model="modal3">
    18. <p>Content of dialog</p>
    19. <p>Content of dialog</p>
    20. <p>Content of dialog</p>
    21. </Modal>
    22. <Button @click="modal4 = true">Internationalization</Button>
    23. <Modal
    24. v-model="modal4"
    25. title="Modal Title"
    26. ok-text="OK"
    27. cancel-text="Cancel">
    28. <p>Something...</p>
    29. <p>Something...</p>
    30. <p>Something...</p>
    31. </Modal>
    32. <Button @click="modal5 = true">Set the width</Button>
    33. <Modal
    34. v-model="modal5"
    35. title="Custom width"
    36. width="300">
    37. <p>Customize width, unit px, default 520px.</p>
    38. <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>
    39. </Modal>
    40. </template>
    41. <script>
    42. export default {
    43. data () {
    44. return {
    45. modal2: false,
    46. modal_loading: false,
    47. modal3: false,
    48. modal4: false,
    49. modal5: false
    50. }
    51. },
    52. methods: {
    53. del () {
    54. this.modal_loading = true;
    55. setTimeout(() => {
    56. this.modal_loading = false;
    57. this.modal2 = false;
    58. this.$Message.success('Successfully delete');
    59. }, 2000);
    60. }
    61. }
    62. }
    63. </script>

    异步关闭

    Modal添加属性loading后,点击确定按钮对话框不会自动消失,并显示 loading 状态,需要手动关闭对话框,常用于表单提交。

    1. <template>
    2. <Button type="primary" @click="modal6 = true">Display dialog box</Button>
    3. <Modal
    4. v-model="modal6"
    5. title="Title"
    6. :loading="loading"
    7. @on-ok="asyncOK">
    8. <p>After you click ok, the dialog box will close in 2 seconds.</p>
    9. </Modal>
    10. <script>
    11. export default {
    12. data () {
    13. return {
    14. modal6: false,
    15. loading: true
    16. },
    17. methods: {
    18. asyncOK () {
    19. setTimeout(() => {
    20. this.modal6 = false;
    21. }, 2000);
    22. }
    23. }
    24. }
    25. </script>

    Modal 对话框 - 图4

    可以禁用关闭和遮罩层关闭。

    自定义位置

    可以自定义 Modal 的对话框样式 以及 对话框 Wrap 的 class 名称,从而实现更多自定义的样式,比如垂直居中。

    1. <style lang="less">
    2. .vertical-center-modal{
    3. display: flex;
    4. align-items: center;
    5. justify-content: center;
    6. .ivu-modal{
    7. top: 0;
    8. }
    9. }
    10. </style>
    11. <template>
    12. <Button @click="modal9 = true">20px from the top</Button>
    13. <Modal
    14. title="Title"
    15. v-model="modal9"
    16. :styles="{top: '20px'}">
    17. <p>Content of dialog</p>
    18. <p>Content of dialog</p>
    19. <p>Content of dialog</p>
    20. </Modal>
    21. <Button @click="modal10 = true">Vertical center</Button>
    22. <Modal
    23. title="Title"
    24. v-model="modal10"
    25. class-name="vertical-center-modal">
    26. <p>Content of dialog</p>
    27. <p>Content of dialog</p>
    28. <p>Content of dialog</p>
    29. </Modal>
    30. </template>
    31. <script>
    32. export default {
    33. data () {
    34. return {
    35. modal9: false,
    36. modal10: false,
    37. }
    38. }
    39. }
    40. </script>

    Modal 对话框 - 图6

    全屏

    设置属性 fullscreen 可以全屏显示。

    设置属性 footer-hide 可以隐藏底部内容。

    1. <template>
    2. <Button @click="modal11 = true">Open a fullscreen modal</Button>
    3. <Modal v-model="modal11" fullscreen title="Fullscreen Modal">
    4. <div>This is a fullscreen modal</div>
    5. </Modal>
    6. </template>
    7. <script>
    8. export default {
    9. data () {
    10. return {
    11. modal11: false
    12. }
    13. }
    14. }
    15. </script>

    拖拽移动

    设置属性 draggable,对话框可以进行拖拽移动。

    实例化使用方法

    除了上述通过标准组件的使用方法,iView 还精心封装了一些实例方法,用来创建一次性的轻量级对话框。

    Modal 对话框 - 图8

    基本用法

    四种基本的对话框,只提供一个确定按钮。

    1. <template>
    2. <Button @click="instance('info')">Info</Button>
    3. <Button @click="instance('success')">Success</Button>
    4. <Button @click="instance('warning')">Warning</Button>
    5. <Button @click="instance('error')">Error</Button>
    6. </template>
    7. <script>
    8. export default {
    9. instance (type) {
    10. const title = 'Title';
    11. const content = '<p>Content of dialog</p><p>Content of dialog</p>';
    12. switch (type) {
    13. case 'info':
    14. title: title,
    15. content: content
    16. });
    17. break;
    18. case 'success':
    19. this.$Modal.success({
    20. title: title,
    21. content: content
    22. });
    23. break;
    24. case 'warning':
    25. this.$Modal.warning({
    26. title: title,
    27. content: content
    28. });
    29. break;
    30. case 'error':
    31. this.$Modal.error({
    32. title: title,
    33. content: content
    34. });
    35. break;
    36. }
    37. }
    38. }
    39. }
    40. </script>

    确认对话框

    快速弹出确认对话框,并且可以自定义按钮文字及异步关闭。

    1. <template>
    2. <Button @click="confirm">Normal</Button>
    3. <Button @click="custom">Custom button text</Button>
    4. <Button @click="async">Asynchronous closing</Button>
    5. </template>
    6. <script>
    7. export default {
    8. methods: {
    9. confirm () {
    10. this.$Modal.confirm({
    11. title: 'Title',
    12. content: '<p>Content of dialog</p><p>Content of dialog</p>',
    13. onOk: () => {
    14. this.$Message.info('Clicked ok');
    15. },
    16. onCancel: () => {
    17. this.$Message.info('Clicked cancel');
    18. }
    19. });
    20. },
    21. custom () {
    22. this.$Modal.confirm({
    23. title: 'Title',
    24. content: '<p>Content of dialog</p><p>Content of dialog</p>',
    25. okText: 'OK',
    26. cancelText: 'Cancel'
    27. });
    28. },
    29. async () {
    30. this.$Modal.confirm({
    31. title: 'Title',
    32. content: '<p>The dialog box will be closed after 2 seconds</p>',
    33. loading: true,
    34. onOk: () => {
    35. setTimeout(() => {
    36. this.$Modal.remove();
    37. this.$Message.info('Asynchronously close the dialog box');
    38. }, 2000);
    39. }
    40. });
    41. }
    42. }
    43. }
    44. </script>

    Modal 对话框 - 图10

    自定义内容

    使用 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()