Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vant Dialog pop-up box


May 07, 2021 Vant


Table of contents


Introduced

Pop-up modal boxes, often used for message prompts, message confirmations, and specific interactions within the current page

Pop-up components support both function calls and component calls

The function is called

Dialog is a function, not a component, so you can call it directly to show the corresponding prompt window

import { Dialog } from 'vant';

Dialog({ message: '提示' });

The component is called

When dialog is called through a component, you can register it in the following way

import Vue from 'vue';
import { Dialog } from 'vant';

// 全局注册
Vue.use(Dialog);

// 局部注册
export default {
  components: {
    [Dialog.Component.name]: Dialog.Component
  }
}

Code demo

Message prompt

Used to prompt for messages that contain only one confirmation button

Dialog.alert({
  title: '标题',
  message: '弹窗内容'
}).then(() => {
  // on close
});

Dialog.alert({
  message: '弹窗内容'
}).then(() => {
  // on close
});

Message confirmation

Used to confirm messages, including cancellation and confirmation buttons

Dialog.confirm({
  title: '标题',
  message: '弹窗内容'
}).then(() => {
  // on confirm
}).catch(() => {
  // on cancel
});

Asynchronous shutdown

function beforeClose(action, done) {
  if (action === 'confirm') {
    setTimeout(done, 1000);
  } else {
    done();
  }
}

Dialog.confirm({
  title: '标题',
  message: '弹窗内容',
  beforeClose
});

The global method

When the Dialog component is introduced, the method is automatically mounted on Vue's prototype$dialog method, which can be called directly within all components

export default {
  mounted() {
    this.$dialog.alert({
      message: '弹窗内容'
    });
  }
}

The component is called

If you need to embed components or other custom content in the bounce window, you can use the way the components are called

<van-dialog v-model="show" title="标题" show-cancel-button>
  <img src="https://img.yzcdn.cn/vant/apple-3.jpg" rel="external nofollow" >
</van-dialog>
export default {
  data() {
    return {
      show: false
    };
  }
}

Api

Method

The method name Description Parameters Returns a value
Dialog Show the marble window options Promise
Dialog.alert Show message prompts e-windows options Promise
Dialog.confirm Show message confirmation e-window options Promise
Dialog.setDefaultOptions Modify the default configuration to take effect for all Dialog options void
Dialog.resetDefaultOptions Reset the default configuration to take effect for all Dialog - void
Dialog.close Close the e-window - void

Options

When dialog is called through a function, the following options are supported:

Parameters Description Type The default
title Title string -
width v2.2.7 Window width, the default unit is px number | string 320px
message Text content, supported \n string -
messageAlign Content alignment, with an optional value of left right string center
className Custom class name any -
showConfirmButton Whether to display the confirmation button boolean true
showCancelButton Whether to display the cancel button boolean false
confirmButtonText Confirm the button document string 确认
confirmButtonColor Confirm the button color string #1989fa
cancelButtonText Cancel the button to do so string 取消
cancelButtonColor Cancel the button color string black
overlay Whether to display the mask layer boolean true
overlayClass v2.2.7 Custom matte layer class name string -
overlayStyle v2.2.7 Custom matte layer styles object -
closeOnPopstate v2.0.5 Whether to close automatically when the page is rolled back boolean false
closeOnClickOverlay Whether to close the e-window after clicking on the mask layer boolean false
lockScroll Whether to lock the background scroll boolean true
beforeClose Callback function before closing,
Close the e-window after calling done(),
Call done (false) to prevent the spring window from closing
(action, done) => void -
transition v2.2.6 The name of the animation class, equivalent to the name name string -
getContainer Specify the mounted node, using an example string | () => Element body

Props

When Dialog is called from a component, the following Props are supported:

Parameters Description Type The default
v-model Whether to display the e-window boolean -
title Title string -
width v2.2.7 Window width, the default unit is px number | string 320px
message Text content, supported \n string -
message-align Content alignment, with an optional value of left right string center
show-confirm-button Whether to display the confirmation button boolean true
show-cancel-button Whether to display the cancel button boolean false
confirm-button-text Confirm the button document string 确认
confirm-button-color Confirm the button color string #1989fa
cancel-button-text Cancel the button to do so string 取消
cancel-button-color Cancel the button color string black
overlay Whether to display the mask layer boolean true
overlay-class v2.2.7 Custom matte layer class name string -
overlay-style v2.2.7 Custom matte layer styles object -
close-on-popstate v2.0.5 Whether to close automatically when the page is rolled back boolean false
close-on-click-overlay Whether to close the e-window after clicking on the mask layer boolean false
lazy-render Whether the node is rendered when the bullet layer is displayed boolean true
lock-scroll Whether to lock the background scroll boolean true
before-close Callback function before closing,
Close the e-window after calling done(),
Call done (false) to prevent the spring window from closing
(action, done) => void -
transition v2.2.6 The name of the animation class, equivalent to the name property of name string -
get-container Specify the mounted node, using an example string | () => Element -

Events

When Dialog is called through a component, the following events are supported:

Event Description Callback parameters
confirm Triggered when the confirmation button is clicked -
cancel Triggered when the cancel button is clicked -
open Triggered when the e-window is opened -
opened Open the e-window and trigger after the animation ends -
close Triggered when the e-window is closed -
closed Close the e-window and trigger after the animation ends -

Slots

When Dialog is called from a component, the following slots are supported:

Name Description
default Custom content
title Custom title


Example demonstration