element-plus/docs/en-US/component/message.md
表弟 b81bf53f0a
feat(components): [el-message] merge the same content messages(#3836) (#3988)
* style(components): [el-message-box] string does not wrap(#3856)

* feat(components): [el-message] merge the same content messages(#3836)

* feat(components): [el-message] merge the same content messages(#3836)

* feat(components): [el-message] merge the same content messages(#3836)

* refactor(components): el-swtich custom texts and icons

* fix: test

* fix: font size

* fix: example margin

* feat(components): el-switch add inline-prompt support

* revert: paly

* docs: update icons

* feat: add inner text

* fix: switch test

* refactor: icon inline

* feat(components): [el-message] merge the same content messages(#3836)

* feat(components): [el-message] merge the same content messages(#3836)

Co-authored-by: Aex <adaex@qq.com>
2021-11-05 18:55:40 +08:00

111 lines
5.4 KiB
Markdown

---
title: Message
lang: en-US
---
# Message
Used to show feedback after an activity. The difference with Notification is that the latter is often used to show a system level passive notification.
## Basic usage
Displays at the top, and disappears after 3 seconds.
:::demo The setup of Message is very similar to notification, so parts of the options won't be explained in detail here. You can check the options table below combined with notification doc to understand it. Element Plus has registered a `$message` method for invoking. Message can take a string or a VNode as parameter, and it will be shown as the main body.
message/basic
:::
## Types
Used to show the feedback of Success, Warning, Message and Error activities.
:::demo When you need more customizations, Message component can also take an object as parameter. For example, setting value of `type` can define different types, and its default is `info`. In such cases the main body is passed in as the value of `message`. Also, we have registered methods for different types, so you can directly call it without passing a type like `open4`.
message/different-types
:::
## Closable
A close button can be added.
:::demo A default Message cannot be closed manually. If you need a closable message, you can set `showClose` field. Besides, same as notification, message has a controllable `duration`. Default duration is 3000 ms, and it won't disappear when set to `0`.
message/closable
:::
## Centered text
Use the `center` attribute to center the text.
:::demo
message/centered-content
:::
## Use HTML string
`message` supports HTML string.
:::demo Set `dangerouslyUseHTMLString` to true and `message` will be treated as an HTML string.
message/raw-html
:::
:::warning
Although `message` property supports HTML strings, dynamically rendering arbitrary HTML on your website can be very dangerous because it can easily lead to [XSS attacks](https://en.wikipedia.org/wiki/Cross-site_scripting). So when `dangerouslyUseHTMLString` is on, please make sure the content of `message` is trusted, and **never** assign `message` to user-provided content.
:::
## grouping
merge messages with the same content.
:::demo Set `grouping` to true and the same content of `message` will be merged.
message/grouping
:::
## Global method
Element Plus has added a global method `$message` for `app.config.globalProperties`. So in a vue instance you can call `Message` like what we did in this page.
## Local import
```ts
import { ElMessage } from 'element-plus'
```
In this case you should call `ElMessage(options)`. We have also registered methods for different types, e.g. `ElMessage.success(options)`. You can call `ElMessage.closeAll()` to manually close all the instances.
## Options
| Attribute | Description | Type | Accepted Values | Default |
| ------------------------ | ------------------------------------------------------------------------------ | -------------------- | -------------------------- | ------------- |
| message | message text | string / VNode | — | — |
| type | message type | string | success/warning/info/error | info |
| icon-class | custom icon's class, overrides `type` | string | — | — |
| dangerouslyUseHTMLString | whether `message` is treated as HTML string | boolean | — | false |
| custom-class | custom class name for Message | string | — | — |
| duration | display duration, millisecond. If set to 0, it will not turn off automatically | number | — | 3000 |
| show-close | whether to show a close button | boolean | — | false |
| center | whether to center the text | boolean | — | false |
| on-close | callback function when closed with the message instance as the parameter | function | — | — |
| offset | set the distance to the top of viewport | number | — | 20 |
| appendTo | set the root element for the message | string / HTMLElement | - | document.body |
| grouping | merge messages with the same content, type of VNode message is not supported | boolean | — | false |
## Methods
`Message` and `this.$message` returns the current Message instance. To manually close the instance, you can call `close` on it.
| Method | Description |
| ---- | ---- |
| close | close the Message |