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. 效果 无法直接发布效果图,自行运行代码查看具体效果

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬加栤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值