useQRCode.ts 464 B

123456789101112131415161718192021222324
  1. import { ComputedRef, Ref, ref, watch } from "vue";
  2. import QRCode from "qrcode";
  3. type MaybeRef<T> = T | Ref<T> | ComputedRef<T>;
  4. const useQRCode = (
  5. text: MaybeRef<string>,
  6. options?: QRCode.QRCodeToDataURLOptions
  7. ) => {
  8. const src = ref(text);
  9. const result = ref("");
  10. watch(
  11. src,
  12. async (value) => {
  13. result.value = await QRCode.toDataURL(value, options);
  14. },
  15. { immediate: true }
  16. );
  17. return result;
  18. };
  19. export default useQRCode;