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. 效果

相关推荐
Y42581 小时前
本地多语言切换具体操作代码
前端·javascript·vue.js
fruge3 小时前
React 2025 完全指南:核心原理、实战技巧与性能优化
javascript·react.js·性能优化
速易达网络4 小时前
Bootstrap 5 响应式网站首页模板
前端·bootstrap·html
etsuyou4 小时前
js前端this指向规则
开发语言·前端·javascript
lichong9514 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
cai_huaer4 小时前
BugKu Web渗透之 cookiesWEB
前端·web安全
lichong9514 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
友友马5 小时前
『 QT 』QT控件属性全解析 (一)
开发语言·前端·qt
不想上班只想要钱5 小时前
vue3+vite创建的项目,运行后没有 Network地址
前端·javascript·vue.js
流***陌5 小时前
手办盲盒抽赏小程序前端功能设计:兼顾收藏需求与抽赏乐趣
前端·小程序