1# qrcode开发指导 2 3 4生成并显示二维码,具体用法请参考[qrcode](../reference/apis-arkui/arkui-js/js-components-basic-qrcode.md)。 5 6 7## 创建qrcode组件 8 9在pages/index目录下的hml文件中创建一个qrcode组件。 10 11 12```html 13<!-- xxx.hml--> 14<div class="container"> 15 <qrcode value="Hello"></qrcode> 16</div> 17``` 18 19 20```css 21/* xxx.css */ 22.container { 23 width: 100%; 24 height: 100%; 25 flex-direction: column; 26 align-items: center; 27 justify-content: center; 28 background-color: #F1F3F5; 29} 30``` 31 32 33 34> **说明:** 35> 36> qrcode组件在创建的时候value的值为必填项。 37 38 39## 设置组件类型 40 41通过设置qrcode的type属性来选择按钮类型,如定义qrcode为矩形二维码、圆形二维码。 42 43 44```html 45<!-- xxx.hml--> 46<div class="container"> 47 <select onchange="settype"> 48 <option for="{{bcol_list}}" value="{{$item}}">{{$item}}</option> 49 </select> 50 <qrcode value="Hello" type="{{qr_type}}"></qrcode> 51</div> 52``` 53 54 55```css 56/* xxx.css */ 57.container { 58 width: 100%; 59 height: 100%; 60 flex-direction: column; 61 align-items: center; 62 justify-content: center; 63 background-color: #F1F3F5; 64} 65select{ 66 margin-top: 50px; 67 margin-bottom: 50px; 68} 69``` 70 71 72```js 73// index.js 74export default { 75 data: { 76 qr_type: 'rect', 77 bcol_list: ['rect','circle'] 78 }, 79 settype(e) { 80 this.qr_type = e.newValue 81 }, 82} 83``` 84 85 86 87 88## 设置样式 89 90通过color和background-color样式为二维码设置显示颜色和背景颜色。 91 92 93```html 94<!-- xxx.hml--> 95<div class="container"> 96 <qrcode value="Hello" type="rect"></qrcode> 97</div> 98``` 99 100 101```css 102/* xxx.css */ 103.container { 104 width: 100%; 105 height: 100%; 106 flex-direction: column; 107 align-items: center; 108 justify-content: center; 109 background-color: #F1F3F5; 110} 111qrcode{ 112 width: 300px; 113 height: 300px; 114 color: blue; background-color: #ffffff; 115} 116``` 117 118 119 120> **说明:** 121> - width和height不一致时,取二者较小值作为二维码的边长,且最终生成的二维码居中显示。 122> 123> - width和height只设置一个时,取设置的值作为二维码的边长。都不设置时,使用200px作为默认边长。 124> 125 126 127## 场景示例 128 129在本场景中将二维码与输入框绑定,通过改变输入框的内容改变二维码。 130 131 132```html 133<!-- xxx.hml--> 134<div class="container"> 135 <input style="margin-bottom: 100px;" onchange="change"></input> 136 <qrcode value="{{textVal}}"></qrcode> 137</div> 138``` 139 140 141```css 142/* xxx.css */ 143.container { 144 width: 100%; 145 height: 100%; 146 flex-direction: column; 147 align-items: center; 148 justify-content: center; 149 background-color: #F1F3F5; 150} 151qrcode{ 152 width: 400px; 153 height: 400px; 154} 155``` 156 157 158```js 159// index.js 160export default{ 161 data: { 162 textVal: '' 163 }, 164 change(e){ 165 this.textVal = e.value 166 } 167} 168``` 169 170 171