js-components-basic-qrcode.md 3.2 KB
Newer Older
E
ester.zhou 已提交
1
# qrcode
Z
zengyawen 已提交
2

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

E
ester.zhou 已提交
7
The **\<qrcode>** component is used to generate and display a QR code.
E
ester.zhou 已提交
8 9

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

None

E
ester.zhou 已提交
13 14

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

Not supported


E
ester.zhou 已提交
19 20 21 22 23 24 25
## Attributes

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

| Name   | Type    | Default Value | Mandatory  | Description                                      |
| ----- | ------ | ---- | ---- | ---------------------------------------- |
| value | string | -    | Yes   | Content used to generate the QR code.                             |
E
ester.zhou 已提交
26
| type  | string | rect | No   | QR code type. Available values are as follows:<br>- **rect**: rectangular QR code.<br>- **circle**: round QR code.|
E
ester.zhou 已提交
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 53 54 55 56 57


## Styles

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

| Name              | Type           | Default Value     | Mandatory  | Description      |
| ---------------- | ------------- | -------- | ---- | -------- |
| color            | &lt;color&gt; | \#000000 | No   | Color of the QR code.  |
| background-color | &lt;color&gt; | \#ffffff | No   | Background color of the QR code.|

>  **NOTE**
>  - If the values of **width** and **height** are different, the smaller value is used as the length of the QR code. The generated QR code is centered.
>
>
>  - If either **width** or **height** is set, the value is used as the length of the QR code. If neither of them is set, the default length 200 px is used.
>


## 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 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<!-- xxx.hml -->
<div class="container">
  <qrcode value="{{qr_value}}" type="{{qr_type}}"
  style="color: {{qr_col}};background-color: {{qr_bcol}};width: {{qr_size}};height: {{qr_size}};margin-bottom: 70px;"></qrcode>
  <text class="txt">Type</text>
  <switch showtext="true" checked="true" texton="rect" textoff="circle" onchange="settype"></switch>
  <text class="txt">Color</text>
  <select onchange="setcol">
    <option for="{{col_list}}" value="{{$item}}">{{$item}}</option>
  </select>
  <text class="txt">Background Color</text>
  <select onchange="setbcol">
    <option for="{{bcol_list}}" value="{{$item}}">{{$item}}</option>
  </select>
</div>
```

E
ester.zhou 已提交
75
```css
Z
zengyawen 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/* xxx.css */
.container {
  width: 100%;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.txt {
  margin: 30px;
  color: orangered;
}
select{
  margin-top: 40px;
  margin-bottom: 40px;
}
Z
zengyawen 已提交
92 93
```

E
ester.zhou 已提交
94
```js
Z
zengyawen 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/* index.js */
export default {
  data: {
    qr_type: 'rect',
    qr_size: '300px',
    qr_col: '#87ceeb',
    col_list: ['#87ceeb','#fa8072','#da70d6','#80ff00ff','#00ff00ff'],
    qr_bcol: '#f0ffff',
    bcol_list: ['#f0ffff','#ffffe0','#d8bfd8']
  },
  settype(e) {
    if (e.checked) {
      this.qr_type = 'rect'
    } else {
      this.qr_type = 'circle'
    }
  },
  setcol(e) {
    this.qr_col = e.newValue
  },
  setbcol(e) {
    this.qr_bcol = e.newValue
  }
}
```

E
ester.zhou 已提交
121
![12](figures/12.gif)