index.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div class="qrcode-container">
  3. <el-card shadow="hover" header="qrcodejs2 二维码生成">
  4. <el-alert
  5. title="感谢优秀的 `qrcodejs2`,项目地址:https://github.com/davidshimjs/qrcodejs"
  6. type="success"
  7. :closable="false"
  8. class="mb15"
  9. ></el-alert>
  10. <div class="qrcode-img-warp">
  11. <div class="mb30 mt30 qrcode-img">
  12. <div class="qrcode" ref="qrcodeRef"></div>
  13. </div>
  14. <el-button type="primary" size="default" @click="onInitQrcode">
  15. <el-icon>
  16. <ele-Refresh />
  17. </el-icon>
  18. 重新生成
  19. </el-button>
  20. </div>
  21. </el-card>
  22. </div>
  23. </template>
  24. <script lang="ts">
  25. import { toRefs, reactive, onMounted, getCurrentInstance, defineComponent } from 'vue';
  26. import QRCode from 'qrcodejs2-fixes';
  27. export default defineComponent({
  28. name: 'funQrcode',
  29. setup() {
  30. const { proxy } = getCurrentInstance() as any;
  31. const state = reactive({
  32. qrcode: '',
  33. });
  34. // 初始化生成二维码
  35. const initQrcode = () => {
  36. new QRCode(proxy.$refs.qrcodeRef, {
  37. text: `https://lyt-top.gitee.io/vue-next-admin-preview/#/login?t=${new Date().getTime()}`,
  38. width: 125,
  39. height: 125,
  40. colorDark: '#000000',
  41. colorLight: '#ffffff',
  42. });
  43. };
  44. // 重新生成
  45. const onInitQrcode = () => {
  46. proxy.$refs.qrcodeRef.innerHTML = '';
  47. initQrcode();
  48. };
  49. // 页面加载时
  50. onMounted(() => {
  51. initQrcode();
  52. });
  53. return {
  54. onInitQrcode,
  55. ...toRefs(state),
  56. };
  57. },
  58. });
  59. </script>
  60. <style scoped lang="scss">
  61. .qrcode-container {
  62. .qrcode-img-warp {
  63. text-align: center;
  64. .qrcode-img {
  65. display: flex;
  66. width: 100%;
  67. height: 125px;
  68. .qrcode {
  69. margin: auto;
  70. width: 125px;
  71. height: 125px;
  72. }
  73. }
  74. }
  75. }
  76. </style>