使用vue2做一个生成二维码的案例【可当组件使用】

最近有个需求需要用前端来生成一个二维码,就封装了一个简单的组件,这篇文章来分享给大家。
使用的技术:
Vue2
Ant Design Vue
QRCodeJS2
node版本:16.20

组件样式:
在这里插入图片描述

大家可以根据自己的需求来调整代码。

在这里插入图片描述
依赖安装:

npm i

项目启动:

npm run serve

调用页面代码:

<template>
  <div id="app">
    <div class="container">
      <a-card title="二维码生成示例" style="width: 500px">
        <a-input
          v-model="qrContent"
          placeholder="请输入二维码内容"
          style="margin-bottom: 10px"
        />
        <a-input
          v-model="qrText"
          placeholder="请输入二维码说明文字"
          style="margin-bottom: 10px"
        />
        <a-input
          v-model="backgroundColor"
          placeholder="请输入二维码背景色(如:#ffffff)"
          style="margin-bottom: 10px"
        />
        <a-button type="primary" @click="generateQR">生成二维码</a-button>
        <a-divider />
        <QRCode
          v-if="showQR"
          ref="qrCode"
          :value="qrContent"
          :text="qrText"
          :qrSize="300"
          :padding="5"
          :fontSize="18"
          :backgroundColor="backgroundColor"
          textAlign="left"
        ></QRCode>
        <a-button v-if="showQR" type="primary" @click="downloadQR" style="margin-top: 10px">下载二维码</a-button>
      </a-card>
    </div>
  </div>
</template>

<script>
import QRCode from './components/QRCode.vue'

export default {
  name: 'App',
  components: {
    QRCode
  },
  data() {
    return {
      qrContent: '',
      qrText: '',
      backgroundColor: '#ffffff',
      showQR: false
    }
  },
  methods: {
    generateQR() {
      if (!this.qrContent) {
        this.$message.error('请输入二维码内容')
        return
      }
      this.showQR = true
    },
    downloadQR() {
      const qrData = this.$refs.qrCode.getQRCodeData()
      const link = document.createElement('a')
      link.download = 'qrcode.png'
      link.href = qrData
      link.click()
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}
</style>

组件核心代码:
在这里插入图片描述

这个使用vue2 生成二维码的案例 代码 已经整理好了,有需要的小伙伴,可以自行获取使用:

https://wwwoop.com/home/Index/projectInfo?goodsId=85&typeParam=2&subKey=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Json____

您的鼓励是我创作的动力~

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

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

打赏作者

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

抵扣说明:

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

余额充值