-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathQRCodeView.vue
66 lines (56 loc) · 1.16 KB
/
QRCodeView.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<template>
<div
ref="qrcode"
:style="style"
/>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
import QRCode from 'easyqrcodejs';
@Component
export default class QRCodeView extends Vue {
/**
* QR code value
*/
@Prop(String) readonly value!: string
/**
* QR code size
*/
@Prop({ default: 100 }) readonly size!: number
/**
* QR code color
*/
@Prop({ default: 'black' }) readonly color!: string
qrcode: QRCode | undefined
get style() {
const width = `${this.size}px`;
return {
width,
height: width,
};
}
mounted() {
this.drawQRCode()
}
@Watch('value')
drawQRCode() {
if (!this.value) return;
if (!this.qrcode) {
this.qrcode = new QRCode(this.$refs.qrcode, {
text: this.value,
width: this.size,
height: this.size,
colorDark: this.color,
colorLight: 'transparent',
drawer: 'svg',
correctLevel: QRCode.CorrectLevel.H,
})
} else {
this.qrcode.makeCode(this.value)
}
}
handleResize() {
window.requestAnimationFrame(this.drawQRCode)
}
}
</script>