js-components-container-badge.md 4.1 KB
Newer Older
E
ester.zhou 已提交
1
# badge
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 5. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7 8 9 10
The **\<badge>** component is used to mark new events that require user attention in your application.


## Required Permissions
Z
zengyawen 已提交
11 12 13

None

E
ester.zhou 已提交
14 15

## Child Components
Z
zengyawen 已提交
16 17 18

This component supports only one child component.

E
ester.zhou 已提交
19 20 21
>  **NOTE**
>
>  If multiple child components are used, only the first one takes effect by default.
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

## Attributes

In addition to the [universal attributes](js-components-common-attributes.md), the following attributes are supported.

| Name                | Type         | Default Value     | Mandatory  | Description                                      |
| ------------------ | ----------- | -------- | ---- | ---------------------------------------- |
| placement          | string      | rightTop | No   | Position of a number or dot badge. Available values are as follows:<br>- **right**: on the right border of the component.<br>- **rightTop**: in the upper right corner of the component border.<br>- **left**: on the left border of the component.|
| count              | number      | 0        | No   | Number of notifications displayed via the badge. The default value is **0**. If the number of notifications is greater than 0, the badge changes from a dot to the number. If this attribute is not set or the value is less than or equal to 0, the badge is a dot.<br>When the **count** value is greater than the **maxcount** value, *maxcount***+** is displayed. The largest integer value supported for **count** is **2147483647**.|
| visible            | boolean     | false    | No   | Whether to display the badge. The value **true** means that the badge shows up when a new notification is received. To use a number badge, set the **count** attribute.|
| maxcount           | number      | 99       | No   | Maximum number of notifications. When the number of new notifications exceeds the value of this attribute, *maxcount***+** is displayed, for example, **99+**.<br>The largest integer value supported for **maxcount** is **2147483647**.|
| config             | BadgeConfig | -        | No   | Configuration of the badge.                          |
| label<sup>6+</sup> | string      | -        | No   | Text of the new notification displayed via the badge.<br>When this attribute is set, attributes **count** and **maxcount** do not take effect.|

**Table 1** BadgeConfig

| Name        | Type            | Default Value     | Mandatory  | Description          |
| ---------- | -------------- | -------- | ---- | ------------ |
| badgeColor | &lt;color&gt;  | \#fa2a2d | No   | Background color of the badge.   |
| textColor  | &lt;color&gt;  | \#ffffff | No   | Text color of the number badge.|
| textSize   | &lt;length&gt; | 10px     | No   | Text size of the number badge.|
| badgeSize  | &lt;length&gt; | 6px      | No   | Default size of the dot badge.   |


## Styles

The [universal styles](../arkui-js/js-components-common-styles.md) are supported.

>  **NOTE**
>
53
>  The total size of child components must be smaller than or equal to that of the **\<badge>** component. Otherwise, the child components cannot be displayed.
E
ester.zhou 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68


## Events

The [universal events](../arkui-js/js-components-common-events.md) are supported.


## Methods

The [universal methods](../arkui-js/js-components-common-methods.md) are supported.


## Example

```html
Z
zengyawen 已提交
69 70 71
<!-- xxx.hml -->
<div class="container">
  <badge class="badge" config="{{badgeconfig}}" visible="true" count="100" maxcount="99">
Z
zengyawen 已提交
72
    <text class="text1">example</text>
Z
zengyawen 已提交
73 74
  </badge>
  <badge class="badge" visible="true" count="0">
Z
zengyawen 已提交
75
    <text class="text2">example</text>
Z
zengyawen 已提交
76 77 78 79
  </badge>
</div>
```

E
ester.zhou 已提交
80
```css
Z
zengyawen 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/* xxx.css */
.container {
  flex-direction: column;
  width: 100%;
  align-items: center;
}
.badge {
  width: 50%;
  margin-top: 100px;
}
.text1 {
  background-color: #f9a01e;
  font-size: 50px;
}
.text2 {
  background-color: #46b1e3;
  font-size: 50px;
}
```

E
ester.zhou 已提交
101
```js
Z
zengyawen 已提交
102 103 104 105 106 107 108 109 110 111 112
// xxx.js
export default {
  data:{
    badgeconfig:{
      badgeColor:"#0a59f7",
      textColor:"#ffffff",
    }
  }
}
```

E
ester.zhou 已提交
113
![figures1](figures/figures1.png)