vue使用qrcodejs2-fix或者qrcodejs2插件生成二维码

1. vue2安装

复制代码
npm i qrcodejs2

1.1. vue3安装

复制代码
npm install qrcodejs2-fix

2. 组件中引入并封装成公共组件,vue3版

复制代码
<template>
    <!-- 二维码生成 -->
    <div class="body-div">
        <div style="width: 100%;height: 100%;" :id="id" :ref="id"></div>
    </div>
</template>

<script lang="ts" setup>
import { defineComponent, reactive, toRefs, computed, watch, onMounted, ref, Ref, createVNode } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import QRCode from 'qrcodejs2-fix'
//获取路由传递的参数
const route = useRoute()
const router = useRouter()
const props = defineProps({  // 可以拿到它的值
    id: {
        type: String,
        required: true
    },
    text: {  // 后端返回的二维码地址
        type: String,
        default: 'http://jindo.dev.naver.com/collie'
    },
    width: {
        type: String,
        default: '128'
    },
    height: {
        type: String,
        default: '128'
    },
    colorDark: {
        type: String,
        default: '#000000'
    },
    colorLight: {
        type: String,
        default: '#ffffff'
    }
})


// 定义数据
const state = reactive({
    qrCode: "",
})


const createQrcode = () => {
    document.getElementById(props.id).innerHTML = '';
    new QRCode(document.getElementById(props.id), {
        text: props.text, //页面地址 ,如果页面需要参数传递请注意哈希模式#
        width: props.width, // 二维码宽度 (不支持100%)
        height: props.height, // 二维码高度 (不支持100%)
        colorDark: props.colorDark,
        colorLight: props.colorLight,
        correctLevel: QRCode.CorrectLevel.H,
    });
}

defineExpose({ createQrcode })


onMounted(() => {
    // userList().then(res => {
    //     console.log(res)
    // })
})


</script>

<style lang="less" scoped>
.body-div {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}
</style>

2. 2组件中引入并封装成公共组件,vue2版

复制代码
<template>
  <div style="width: 100%;height: 100%;" :id="id" :ref="id"></div>
</template>
<script>
import QRCode from 'qrcodejs2'
export default {
  data() {
    return {
      qrcode: ''
    }
  },
  props: {
    id: {
      type: String,
      required: true
    },
    text: {  // 后端返回的二维码地址
      type: String,
      default: 'http://jindo.dev.naver.com/collie'
    },
    width: {
      type: String,
      default: '128'
    },
    height: {
      type: String,
      default: '128'
    },
    colorDark: {
      type: String,
      default: '#000000'
    },
    colorLight: {
      type: String,
      default: '#ffffff'
    }
  },
  watch: {
    text(newText) {
      this.createQrcode()
    }
  },
  mounted() {
    this.createQrcode()
  },
  methods: {
    createQrcode() {
      if(this.qrcode) {  // 有新的二维码地址了,先把之前的清除掉
        this.$refs[this.id].innerHTML = ''
      }
      this.qrcode = new QRCode(this.$refs[this.id], {
        text: this.text, //页面地址 ,如果页面需要参数传递请注意哈希模式#
        width: this.width, // 二维码宽度 (不支持100%)
        height: this.height, // 二维码高度 (不支持100%)
        colorDark: this.colorDark,
        colorLight: this.colorLight,
        correctLevel: QRCode.CorrectLevel.H,
      })
    },
    // 制作另一个二维码
    updateCode() {
      this.qrcode.makeCode("http://naver.com")
    }
  }
}
</script>

3. 组件中使用,我的组件是全局注册所以直接引用即可,v2,v3都一样

复制代码
 <Q-code id="qrCode" :text="state.link" :width="100" :height="100" ref="qrCode" />

4. 效果

相关推荐
盛夏绽放2 小时前
jQuery 知识点复习总览
前端·javascript·jquery
胡gh4 小时前
依旧性能优化,如何在浅比较上做文章,memo 满天飞,谁在裸奔?
前端·react.js·面试
大怪v4 小时前
超赞👍!优秀前端佬的电子布洛芬技术网站!
前端·javascript·vue.js
胡gh4 小时前
你一般用哪些状态管理库?别担心,Zustand和Redux就能说个10分钟
前端·面试·node.js
项目題供诗5 小时前
React学习(十二)
javascript·学习·react.js
无羡仙5 小时前
Webpack 背后做了什么?
javascript·webpack
老华带你飞6 小时前
校园交友|基于SprinBoot+vue的校园交友网站(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园交友网站
roamingcode6 小时前
Claude Code NPM 包发布命令
前端·npm·node.js·claude·自定义指令·claude code
码哥DFS6 小时前
NPM模块化总结
前端·javascript
灵感__idea6 小时前
JavaScript高级程序设计(第5版):代码整洁之道
前端·javascript·程序员