4.2 KiB
4.2 KiB
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.
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.
import { showNotify } from 'vant';
showNotify('Notify Message');
Usage
Basic Usage
import { showNotify, closeNotify } from 'vant';
// auto close after 3s
showNotify('Message');
// manually close
closeNotify();
Notify Type
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
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
<van-button type="primary" text="Use Notify Component" @click="showNotify" />
<van-notify v-model:show="show" type="success">
<van-icon name="bell" style="margin-right: 4px;" />
<span>Content</span>
</van-notify>
import { ref } from 'vue';
export default {
setup() {
const show = ref(false);
const showNotify = () => {
show.value = true;
setTimeout(() => {
show.value = false;
}, 2000);
};
return {
show,
showNotify,
};
},
};
API
Methods
Vant exports following Notify utility functions:
| Methods | Description | Attribute | Return value |
|---|---|---|---|
| showNotify | Show notify | NotifyOptions | string |
notify instance |
| closeNotify | Close notify | - | void |
| setNotifyDefaultOptions | Set default options of all notifies | NotifyOptions |
void |
| resetNotifyDefaultOptions | Reset default options of all notifies | - | void |
NotifyOptions
| Attribute | Description | Type | Default |
|---|---|---|---|
| type | Can be set to primary success warning |
NotifyType | danger |
| message | Message | string | - |
| duration | Duration(ms), won't disappear if value is 0 | number | string | 3000 |
| z-index | Set the z-index to a fixed value | number | string | 2000+ |
| position | Position, can be set to bottom |
NotifyPosition | top |
| color | Message color | string | white |
| background | Background color | string | - |
| className | Custom className | string | Array | object | - |
| lockScroll | Whether to lock background scroll | boolean | false |
| onClick | Callback function after click | (event: MouseEvent) => void | - |
| onOpened | Callback function after opened | () => void | - |
| onClose | Callback function after close | () => void | - |
Types
The component exports the following type definitions:
import type {
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.
| Name | Default Value | Description |
|---|---|---|
| --van-notify-text-color | var(--van-white) | - |
| --van-notify-padding | var(--van-padding-xs) var(--van-padding-md) | - |
| --van-notify-font-size | var(--van-font-size-md) | - |
| --van-notify-line-height | var(--van-line-height-md) | - |
| --van-notify-primary-background | var(--van-primary-color) | - |
| --van-notify-success-background | var(--van-success-color) | - |
| --van-notify-danger-background | var(--van-danger-color) | - |
| --van-notify-warning-background | var(--van-warning-color) | - |