首页 > 其他分享 >封装动态生成二维码组件

封装动态生成二维码组件

时间:2024-03-13 17:01:25浏览次数:19  
标签:封装 String default newVal 二维码 generateCode 组件 type

1.封装一个根据内容动态生成二维码的组件

2.效果图如下

3.实现方式有多种展示两种

  •  使用 QRCode 插件
  • 使用 vue-qr 插件

4.组件API

 1. logoSrc设置二维码中心logo 

4.组件API

5.组件代码

  •  QRCode
<template>
    <div class="count-to-wrapper">
        <div ref="canvasWrapper" id="canvasWrapper"></div>
    </div>
</template>
  
<script>
import QRCode from 'qrcode'
export default {
    name: 'QRCode',
    props: {
        value: {
            type: String,
            default: ''
        },
        size: {
            type: Number,
            default: ''
        },  
        errorCorrectionLevel: {
            type: String,
            default: 'L'
        },
        colorDark: {
            type: String,
            default: '#000000'
        },
        colorLight: {
            type: String,
            default: '#ffffff'
        },
        margin: {
            type: String,
            default: '1'
        },
        // logoImage: {
        //     type: String,
        //     default: ''
        // },
        // type: {
        //     type: String,
        //     default: 'canvas'
        // },
    },
    data() {
        return {
            logoImage: require('./icon/icon.png')
        }
    },
    watch: {
        // 二维码跳转地址
        value(newVal) {
            console.log(newVal);
            if (newVal !== '') {
                this.generateCode()
            }
        },
        // 二维码大小
        size(newVal) {
            console.log(newVal);
            this.generateCode()
        },
        // 二维码纠错等级
        errorCorrectionLevel(newVal) {
            console.log(newVal);
            this.generateCode()
        },
        colorDark(newVal) {
            console.log(newVal);
            this.generateCode()
        },
        colorLight(newVal) {
            console.log(newVal);
            this.generateCode()
        },
        margin(newVal) {
            console.log(newVal);
            this.generateCode()
        },

    },
    mounted() {
        this.generateCode()
    },
    methods: {
        generateCode() {
            this.$refs.canvasWrapper.innerHTML = ''
            let opts = {
                errorCorrectionLevel: this.errorCorrectionLevel, //容错级别
                type: "image/png", //生成的二维码类型
                quality: 0.3, //二维码质量
                margin: this.margin, //二维码留白边距
                maskPattern: 0,
                width: this.size ? this.size : 130, //宽
                height: this.size ? this.size : 130, //高
                color: {
                    dark: this.colorDark, //前景色
                    light: this.colorLight, //背景色
                },
                // imageUrl: require("./icon/icon.png"), //欲嵌入至二维码中心的 LOGO 地址
            };
            console.log(opts);
            let canvasimg
            QRCode.toCanvas(this.value, opts, (error, canvas) => {
                if (error) {
                    console.error(error)
                    return
                }
                canvasimg = canvas
                // 在canvas的父元素中插入canvas元素
                this.$refs.canvasWrapper.appendChild(canvas)
            })
            console.log(canvasimg);
            // 添加二维码中间的图片
        }
    }
}
</script>

  
  •  vue-qr
<template>
    <div class="count-to-wrapper">
        <vue-qr :text="value" :size="size" :colorDark="colorDark" :colorLight="colorLight" :correctLevel="correctLevel"
            :margin="margin" :logoSrc="logoSrc" :logoMargin="logoMargin" :logoCornerRadius="logoCornerRadius"
            :backgroundColor="backgroundColor"></vue-qr>
    </div>
</template>
  
<script>
import VueQr from 'vue-qr'
export default {
    name: 'QRCodeConvert',
    components: { VueQr },
    props: {
        // 二维码跳转地址
        value: {
            type: String,
            default: ''
        },
        // 二维码大小
        size: {
            type: Number,
            default: ''
        },
        correctLevel: {
            type: Number,
            default: 1
        },
        colorDark: {
            type: String,
            default: '#000000'
        },
        colorLight: {
            type: String,
            default: '#ffffff'
        },
        margin: {
            type: Number,
            default: 0
        },
        logoSrc: {
            type: String,
            default: ""
        },
        logoMargin: {
            type: Number,
            default: 0
        },
        logoCornerRadius: {
            type: Number,
            default: 5
        },
        backgroundColor: {
            type: String,
            default: ""
        },
        // bgSrc: {
        //     type: String,
        //     default: ""
        // },
    },
    data() {
        return {
        }
    },
    watch: {
    },
    mounted() {
        // this.generateCode()
    },
    methods: {
    }
}
</script>

  

6.使用时引入改组件或者把塌定义为全局组件

标签:封装,String,default,newVal,二维码,generateCode,组件,type
From: https://blog.csdn.net/weixin_61334593/article/details/136682239

相关文章

  • Vue学习笔记50--组件自定义事件
    props--将子组件的信息传递给父组件 <!--通过父组件给子组件传递函数类型的props实现:子给父传递数据-->  <School:getShcoolName="getShcoolName"></School>示例一:App.vue<template><divclass="app"><!--<imgsrc="./assets......
  • 3.安装uview组件
    官网地址https://vkuviewdoc.fsq.pub/https://vkuviewdoc.fsq.pub/components/install.htmluViewVue3.0横空出世,继承uView1.0意志,再战江湖,风云再起!同时支持Vue3.0和Vue2.0,你没看错,现在uView支持Vue3.0了!(不支持nvue,此版本为uView1.0的分支)插件市场https://ext......
  • 其他功能组件 LL
    default_limit默认限制,默认值与PAGE_SIZE设置一直limit_query_paramlimit参数名,默认'limit'offset_query_paramoffset参数名,默认'offset'max_limit最大limit限制,默认None 1、过滤Filtering对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-fitlter扩展来......
  • 2024年最受欢迎的Vue.js组件库 - ViewDesign全面解析
    引言作为现代Web开发不可或缺的一员,Vue.js以其轻量、高效、渐进式的理念备受开发者青睐。而在Vue.js生态系统中,第三方组件库则扮演着桥梁的角色,为开发者提供可复用的UI组件,从而极大提高了开发效率。在2024年,有许多优秀的Vue组件库脱颖而出,但毫无疑问,ViewDesign因其卓越的......
  • 频率组件 LL
    1、throttle"""系统:1)AnonRateThrottle:对同一IP游客的限制2)UserRateThrottle:对同一IP登录用户的限制必须在settings.py中'DEFAULT_THROTTLE_RATES':{'user':'10/min',#登录的用户一分钟可以访问10次'anon':'3/min',#......
  • 认证组件 LL
    1、authentication"""系统:session认证rest_framework.authentication.SessionAuthenticationajax请求通过认证:cookie中要携带sessionid、csrftoken,请求头中要携带x-csrftoken第三方:jwt认证rest_framework_jwt.authentication.JSONWebTokenAuthenticationajax请求通过......
  • 权限组件 LL
    1、permission"""系统:1)AllowAny:允许所有用户,校验方法直接返回True2)IsAuthenticated:只允许登录用户必须request.user和request.user.is_authenticated都通过3)IsAuthenticatedOrReadOnly:游客只读,登录用户无限制get、option、head请求无限制前台请求必须校验......
  • springcloud 基础组件-Feign 调用流程
    原图来自:https://blog.csdn.net/luanlouis/article/details/82821294所有注册的feignclientorg.springframework.cloud.openfeign.FeignAutoConfiguration#feignContext决定使用哪种client的地方org.springframework.cloud.openfeign.FeignAutoConfiguration.OkHttpFeignCo......
  • 07-ElementPlus组件库
    ElementPlus简介ElementPlus是饿了么团队研发的,基于Vue3的组件库准备工作:创建工程化的Vue项目选择TypeScript参照官方文档安装ElementPlus组件库(当前工程的目录下)npminstallelement-plus--savemain.ts中引入ElementPlus组件库参照官方文档//main.tsimpor......
  • 06-TDesign组件库
    TDesign具有统一的价值观,一致的设计语言和视觉风格,帮助用户形成连续、统一的体验认知。在此基础上,TDesign提供了开箱即用的UI组件库、设计指南和相关设计资产,以优雅高效的方式将设计和研发从重复劳动中解放出来,同时方便大家在TDesign的基础上扩展,更好的的贴近业务需求。......