I'm struggling with a challenge that involves creating a QRCode component using Vue 3 and Typescript. Here's the code snippet I've worked on:
<template>
<canvas ref="qrcodeVue"> </canvas>
</template>
<script lang="ts">
import QRCode from "qrcode";
import { Vue, Options } from "vue-class-component";
import { ref } from "vue";
@Options({
props: {
value: {
type: String,
required: true
},
size: {
type: [Number, String],
validator: (s: [number | string]) => isNaN(Number(s)) !== true
},
level: {
type: String,
validator: (l: string) => ["L", "Q", "M", "H"].indexOf(l) > -1
},
background: String,
foreground: String
}
})
export default class QRCodeVue extends Vue {
value = "";
size: number | string = 100;
level: "L" | "Q" | "M" | "H" = "L";
background = "#ffffff";
foreground = "#0000ff";
mounted() {
const _size = Number(this.size);
const scale = window.devicePixelRatio || 1;
const qrcodeVue = ref<HTMLCanvasElement | null>(null);
QRCode.toCanvas(qrcodeVue, this.value, {
scale: scale,
width: _size,
color: { dark: this.foreground, light: this.background },
errorCorrectionLevel: this.level
});
}
}
</script>
However, I'm facing an issue where qrcodeVue
seems to be referencing nothing, preventing me from accessing the canvas itself. Can anyone point out what I might be missing? Where should I place this ref()
code? I also attempted using defineComponent
, but encountered the same problem. Any insights would be highly appreciated.
(On a side note, I also experimented with the npm package qrcode-vue
, but it appears to lack support for Vue 3)