1# qrcode
2
3
4生成并显示二维码。
5
6> **说明:**
7>
8> 该组件从从API version 5 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
9
10
11## 子组件
12
13不支持。
14
15
16## 属性
17
18| 名称 | 类型 | 默认值 | 必填 | 描述 |
19| -------- | -------- | -------- | -------- | -------- |
20| value | string | - | 是 | 用来生成二维码的内容。最大长度为256。 |
21| id | string | - | 否 | 组件的唯一标识。 |
22| style | string | - | 否 | 组件的样式声明。 |
23| class | string | - | 否 | 组件的样式类,用于引用样式表。 |
24| ref | string | - | 否 | 用来指定指向子元素的引用信息,该引用将注册到父组件的$refs 属性对象上。 |
25
26
27## 事件
28
29| 名称 | 参数 | 描述 |
30| -------- | -------- | -------- |
31| click | - | 点击动作触发该事件。 |
32| longpress | - | 长按动作触发该事件。 |
33| swipe<sup>5+</sup> | [SwipeEvent](js-lite-common-events.md) | 组件上快速滑动后触发。 |
34
35
36## 样式
37
38| 名称 | 类型 | 默认值 | 必填 | 描述 |
39| -------- | -------- | -------- | -------- | -------- |
40| color | &lt;color&gt; | \#000000 | 否 | 二维码颜色 |
41| background-color | &lt;color&gt; | \#ffffff | 否 | 二维码背景颜色 |
42| width | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | - | 否 | 设置组件自身的宽度。<br/><br/>未设置时组件宽度默认为0。 |
43| height | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | - | 否 | 设置组件自身的高度。<br/><br/>未设置时组件高度默认为0。 |
44| padding | &lt;length&gt; | 0 | 否 | 使用简写属性设置所有的内边距属性。<br/>&nbsp;&nbsp;该属性可以有1到4个值:<br/>-&nbsp;指定一个值时,该值指定四个边的内边距。<br/>-&nbsp;指定两个值时,第一个值指定上下两边的内边距,第二个指定左右两边的内边距。<br/>-&nbsp;指定三个值时,第一个指定上边的内边距,第二个指定左右两边的内边距,第三个指定下边的内边距。<br/>-&nbsp;指定四个值时分别为上、右、下、左边的内边距(顺时针顺序)。 |
45| padding-[left\|top\|right\|bottom] | &lt;length&gt; | 0 | 否 | 设置左、上、右、下内边距属性。 |
46| margin | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | 0 | 否 | 使用简写属性设置所有的外边距属性,该属性可以有1到4个值。<br/>-&nbsp;只有一个值时,这个值会被指定给全部的四个边。<br/>-&nbsp;两个值时,第一个值被匹配给上和下,第二个值被匹配给左和右。<br/>-&nbsp;三个值时,第一个值被匹配给上,&nbsp;第二个值被匹配给左和右,第三个值被匹配给下。<br/>-&nbsp;四个值时,会依次按上、右、下、左的顺序匹配&nbsp;(即顺时针顺序)。 |
47| margin-[left\|top\|right\|bottom] | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>5+</sup> | 0 | 否 | 设置左、上、右、下外边距属性。 |
48| border-width | &lt;length&gt; | 0 | 否 | 使用简写属性设置元素的所有边框宽度。 |
49| border-color | &lt;color&gt; | black | 否 | 使用简写属性设置元素的所有边框颜色。 |
50| border-radius | &lt;length&gt; | - | 否 | border-radius属性是设置元素的外边框圆角半径。 |
51| display | string | flex | 否 | 确定一个元素所产生的框的类型,可选值为:<br/>-&nbsp;flex:弹性布局。<br/>-&nbsp;none:不渲染此元素。 |
52| [left\|top] | &lt;length&gt;&nbsp;\|&nbsp;&lt;percentage&gt;<sup>6+</sup> | - | 否 | left\|top确定元素的偏移位置。<br/>-&nbsp;left属性规定元素的左边缘。该属性定义了定位元素左外边距边界与其包含块左边界之间的偏移。<br/>-&nbsp;top属性规定元素的顶部边缘。该属性定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。 |
53
54>  **说明:**
55> - width和height不一致时,以二者最小值作为二维码的边长。且最终生成的二维码居中显示;
56>
57>- width和height的最小值为200px。
58
59
60## 示例
61
62
63```html
64<!-- xxx.hml -->
65<div class="container">
66    <qrcode value="{{qr_value}}" class="qrCode" style="color: {{qr_col}};background-color: {{qr_bcol}};"></qrcode>
67    <input type="button" onclick="changeColor" class="button">Color</input>
68    <input type="button" onclick="changeBackgroundColor" class="button">BackgroundColor</input>
69    <input type="button" onclick="changeValue" class="button">Value</input>
70</div>
71```
72
73```css
74/* xxx.css */
75.container {
76  width: 100%;
77  height: 100%;
78  flex-direction: column;
79  justify-content: center;
80  align-items: center;
81}
82.qrCode {
83  width: 200px;
84  height: 200px;
85}
86.button {
87  width: 30%;
88  height: 10%;
89  margin-top: 5%;
90}
91```
92
93```javascript
94// xxx.js
95export default {
96    data: {
97        qr_col: '#87ceeb',
98        qr_bcol: '#f0ffff',
99        qr_value: 'value'
100    },
101    changeColor() {
102        if (this.qr_col == '#87ceeb') {
103            this.qr_col = '#fa8072';
104        } else {
105            this.qr_col = '#87ceeb';
106        }
107    },
108    changeBackgroundColor() {
109        if (this.qr_bcol == '#f0ffff') {
110            this.qr_bcol = '#ffffe0';
111        } else {
112            this.qr_bcol = '#f0ffff';
113        }
114    },
115    changeValue() {
116        if (this.qr_value == 'value') {
117            this.qr_value = 'change';
118        } else {
119            this.qr_value = 'value';
120        }
121    }
122}
123```
124
125![qrcode](figures/qrcode-lite.gif)