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

相关推荐
一颗花生米。15 分钟前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
学习使我快乐0119 分钟前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
bobostudio199520 分钟前
TypeScript 设计模式之【策略模式】
前端·javascript·设计模式·typescript·策略模式
勿语&1 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
黄尚圈圈1 小时前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
浮华似水2 小时前
简洁之道 - React Hook Form
前端
正小安4 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch6 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光6 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   6 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发