mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-11-06 13:32:08 +08:00
Notify
Intro
The display message prompt is at the top of the page, and supports two methods: function call and component 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);
Usage
Basic Usage
Notify('Notify Message');
Notify Type
Notify({ type: 'primary', message: 'Notify Message' });
Notify({ type: 'success', message: 'Notify Message' });
Notify({ type: 'danger', message: 'Notify Message' });
Notify({ type: 'warning', message: 'Notify Message' });
Custom Notify
Notify({
message: 'Custom Color',
color: '#ad0000',
background: '#ffe1e1',
});
Notify({
message: 'Custom Duration',
duration: 1000,
});
Global Method
After registering the Notify component through app.use, the $notify method will be automatically mounted on all subcomponents of the app.
export default {
mounted() {
this.$notify('Notify Message');
},
};
Component Call
<van-button type="primary" text="Component Call" @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
| Methods | Attribute | Return value | Description |
|---|---|---|---|
| Notify | `options | message` | notify instance |
| Notify.clear | - | void |
Close notify |
| Notify.setDefaultOptions | options |
void |
Set default options of all notifies |
| Notify.resetDefaultOptions | - | void |
Reset default options of all notifies |
Options
| Attribute | Description | Type | Default |
|---|---|---|---|
| type | Can be set to primary success warning |
string | danger |
| message | Message | string | - |
| duration | Duration(ms), won't disappear if value is 0 | number | string | 3000 |
| color | Message color | string | white |
| background | Background color | string | - |
| className | Custom className | string | Array | object | - |
lockScroll v3.0.7 |
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 | - |
Less Variables
How to use: Custom Theme.
| Name | Default Value | Description |
|---|---|---|
| @notify-text-color | @white |
- |
| @notify-padding | @padding-xs @padding-md |
- |
| @notify-font-size | @font-size-md |
- |
| @notify-line-height | @line-height-md |
- |
| @notify-primary-background-color | @blue |
- |
| @notify-success-background-color | @green |
- |
| @notify-danger-background-color | @red |
- |
| @notify-warning-background-color | @orange |
- |