組件的形式創建
1.下載依賴
npm install qrcodejs2
2.創建一個.vue的組件放置代碼(我創建的是qrcodejs2.vue)
1 //template中的代碼 2 <template> 3 <div class="boxshow"> 4 <div class="qrcode" ref="qrcodeContainer"></div> 5 </div> 6 </template> 7 <script> 8 import QRCode from 'qrcodejs2' // 引入qrcode 9 export default { 10 name: 'test', 11 props: { 12 QRCodetext: { 13 type: String,//類型限定 14 default: '' //默認 15 } 16 }, 17 mounted() { 18 this.$nextTick(() => { 19 this.qrcode() 20 }) 21 }, 22 methods: { 23 qrcode() { 24 let qrcode = new QRCode(this.$refs.qrcodeContainer, { 25 width: 100,// 二維碼的寬 26 height: 100,// 二維碼的高 27 text: this.QRCodetext ? this.QRCodetext : '', // 二維碼的內容 28 colorDark: '#000',// 二維碼的顏色 29 colorLight: '#fff', 30 correctLevel: QRCode.CorrectLevel.H 31 }) 32 } 33 } 34 } 35 </script>
通過qrcodejs2生成的二維碼本身是沒有白邊的
如果想像這樣生成一個有邊框的的二維碼,更好看一點 我這邊用了box-shadow屬性修改后 看起來是不是高大上一點 哈哈哈哈哈哈哈
上代碼 這是需要安裝less的插件 也可以不用
1 npm install less
1 <style lang='less'> 2 .boxShadow { 3 padding: 20px 0 0 40px; 4 .qrcode { 5 box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3); 6 display: inline-block; 7 img { 8 width: 132px; 9 height: 132px; 10 background-color: #fff; //設置白色背景色也可以設置別的看業務需求 11 padding: 6px; // 利用padding 12 } 13 } 14 } 15 </style> 16 17 //不用less的 注意要更有針對性 避免污染 18 <style> 19 .boxShadow { 20 padding: 20px 0 0 40px; 21 } 22 .qrcode { 23 box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3); 24 display: inline-block; 25 } 26 img { 27 width: 132px; 28 height: 132px; 29 background-color: #fff;/* 設置白色背景色*/ 30 padding: 6px; 31 } 32 </style>
3.使用的的話直接引入 祖冊即可
1 <template> 2 <QRCode ref="qrcode" :QRCodetext="QRCodetext"></QRCode> 3 </template> 4 <script> 5 import QRCode from '../compon/qrcodejs2' 6 export default { 7 name: '', 8 data() { 9 return { 10 QRCodetext: '', 11 } 12 }, 13 created() { 14 this.QRCodetext = 'www.baidu.com'//想要的內容 15 }, 16 mounted() {}, 17 methods: {}, 18 components: { QRCode } 19 } 20 </script>
ok!去玩耍吧