Skip to content

Notify - Vant 4

Notify

Intro

The display message prompt is at the top of the page, and supports two methods: component call and function call.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

js
import { createApp } from'vue'; import { Notify } from'vant'; const app = createApp(); app.use(Notify);

Function Call

Vant provides some utility functions that can quickly evoke global Notify components.

For example, calling the showNotify function will render a Dialog directly in the page.

js
import { showNotify } from'vant'; showNotify('Notify Message');

Usage

Basic Usage

js
import { showNotify, closeNotify } from'vant'; // auto close after 3sshowNotify('Message'); // manually closecloseNotify();

Notify Type

js
import { showNotify } from'vant'; showNotify({ type: 'primary', message: 'Notify Message' }); showNotify({ type: 'success', message: 'Notify Message' }); showNotify({ type: 'danger', message: 'Notify Message' }); showNotify({ type: 'warning', message: 'Notify Message' });

Custom Notify

js
import { showNotify } from'vant'; showNotify({ message: 'Custom Color', color: '#ad0000', background: '#ffe1e1', }); showNotify({ message: 'Custom Position', position: 'bottom', }); showNotify({ message: 'Custom Duration', duration: 1000, });

Use Notify Component

html
js
import { ref } from'vue'; exportdefault { setup() { const show = ref(false); constshowNotify = () => { show.value = true; setTimeout(() => { show.value = false; }, 2000); }; return { show, showNotify, }; }, };

API

Methods

Vant exports following Notify utility functions:

MethodsDescriptionAttributeReturn value
showNotifyDisplay Notify at the top of the page`NotifyOptionsstring`
closeNotifyClose the currently displayed Notify-void
setNotifyDefaultOptionsModify the default configuration, affecting all showNotify callsNotifyOptionsvoid
resetNotifyDefaultOptionsReset the default configuration, affecting all showNotify calls-void

NotifyOptions

When calling the showNotify and other related methods, the following options are supported:

AttributeDescriptionTypeDefault
typeCan be set to primary``success``warningNotifyTypedanger
messageMessagestring-
durationDuration(ms), won't disappear if value is 0*numberstring*
zIndexSet the z-index to a fixed value*numberstring*
positionPosition, can be set to bottomNotifyPositiontop
colorMessage colorstringwhite
backgroundBackground colorstring-
classNameCustom className*stringArray
lockScrollWhether to lock background scrollbooleanfalse
teleportSpecifies a target element where Notify will be mounted*stringElement*
onClickCallback function after click(event: MouseEvent) => void-
onOpenedCallback function after opened() => void-
onCloseCallback function after close() => void-

Props

When using Notify as a component, the following props are supported:

AttributeDescriptionTypeDefault
v-model:showWhether to show notifybooleanfalse
typeCan be set to primary``success``warningNotifyTypedanger
messageMessagestring-
z-indexSet the z-index to a fixed value*numberstring*
positionPosition, can be set to bottomNotifyPositiontop
colorMessage colorstringwhite
backgroundBackground colorstring-
class-nameCustom className*stringArray
lock-scrollWhether to lock background scrollbooleanfalse
teleportSpecifies a target element where Notify will be mounted*stringElement*

Events

When using Notify as a component, the following events are supported:

EventDescriptionParameters
clickCallback function after clickevent: MouseEvent
closeCallback function after close-
openedCallback function after opened-

Slots

When using Notify as a component, the following slots are supported:

NameDescription
defaultCustom content

Types

The component exports the following type definitions:

ts
importtype { NotifyType, NotifyProps, NotifyOptions, NotifyPosition, } from'vant';

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-notify-text-colorvar(--van-white)-
--van-notify-paddingvar(--van-padding-xs) var(--van-padding-md)-
--van-notify-font-sizevar(--van-font-size-md)-
--van-notify-line-heightvar(--van-line-height-md)-
--van-notify-primary-backgroundvar(--van-primary-color)-
--van-notify-success-backgroundvar(--van-success-color)-
--van-notify-danger-backgroundvar(--van-danger-color)-
--van-notify-warning-backgroundvar(--van-warning-color)-

Enterprise-level mobile solution based on Vant