qrcodejs2--Vue生成二维码组件封装

1.安装qrcodejs2:

npm install  qrcodejs2 --save
yarn add  qrcodejs2

2.初步封装组件:

/**
 * @file 生成二维码的组件
 * @author Walker
 * @date 2020-03-16
 */
<template>
  <div class="emqrcode">
    <button @click="showQRcode">点击分享二维码</button>
    <div id="qrcode" ref="qrcode"></div>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  components: { QRCode },
  data() {
    return {
      link: "https://www.baidu.com/"
    };
  },
  methods: {
    /**
     * @description 生成二维码
     * @param  {number} qWidth  宽度
     * @param  {number} qHeight  高度
     * @param  {string} qText  二维码内容(跳转连接)
     * @param  {string} qRender 渲染方式(有两种方式 table和canvas,默认是canvas)
     */
    qrcode(qWidth, qHeight, qText, qRender) {
      let qrcode = new QRCode("qrcode", {
        width: qWidth,
        height: qHeight,
        text: qText,
        render: qRender
      });
    },

    /**
     * @description 点击显示二维码
     */
    showQRcode() {
      //二维码初始化 点击一次添加一个二维码
      this.$refs.qrcode.innerHTML = "";
      this.$nextTick(function() {
        this.qrcode(124, 124, this.link, "canvas");
      });
    }
  }
};
</script>

<style lang="less">
.emqrcode {
  width: 100%;
  background-color: #f00;
}
</style>

最后的样式:

在这里插入图片描述

3.结合ElementUI:

/**
 * @file 生成二维码的组件
 * @author Walker
 * @date 2020-03-16
 */
<template>
  <div class="emqrcode">
    <el-button type="primary" class="left_transition" @click="showShare = !showShare">
      <i class="el-icon-caret-left"></i>
    </el-button>

    <div class="share_box">
      <transition name="el-zoom-in-center">
        <div v-show="showShare" class="transition-box">
          <el-button type="text" class="share_QRcode" @click="showQRcode">点击分享二维码</el-button>
        </div>
      </transition>
    </div>

    <el-dialog
      title="分享二维码"
      custom-class="dialog_style"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
      @open="showQRcode"
    >
      <div :append-to-body="false" id="qrcode" ref="qrcode"></div>
      <span slot="footer" class="dialog-footer">
        <a class="linl_style">复制链接:{{link}}</a>
        <!-- <el-button @click="centerDialogVisible = false">取 消</el-button> -->
        <!-- <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button> -->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  components: { QRCode },
  data() {
    return {
      link:"https://www.baidu.com/",
      centerDialogVisible: false,
      showShare: false
    };
  },
  methods: {
    /**
     * @description 生成二维码
     * @param  {number} qWidth  宽度px
     * @param  {number} qHeight  高度px
     * @param  {string} qText  二维码内容(跳转连接)
     * @param  {string} qRender 渲染方式(有两种方式 table和canvas,默认是canvas)
     */
    qrcode(qWidth, qHeight, qText, qRender) {
      let qrcode = new QRCode("qrcode", {
        width: qWidth,
        height: qHeight,
        text: qText,
        render: qRender
      });
    },

    /**
     * @description 遮罩打开的回调 点击显示二维码
     */
    showQRcode() {
      //收回抽屉
      this.showShare = false;
      //调用函数生成二维码
      this.$nextTick(function() {
      	//二维码初始化 点击一次添加一个二维码
        this.$refs.qrcode.innerHTML = "";
        this.qrcode(124, 124, this.link, "canvas");
      });
	  //打开遮罩
      this.centerDialogVisible = true;
    }
  }
};
</script>

<style lang="less" scoped>
.left_transition {
  border-radius: 0;
  border: none;
  border-right: 1px solid #ccc;
  background-color: #4157ff;
  height: 36px;
  padding: 0 4px 0 3px;
}
.share_box {
  display: inline-block;
  height: 36px;
  border: none;
  vertical-align: top;
}
.emqrcode {
  position: fixed;
  right: 17px;
  top: 20px;
  width: auto;
  // background-color: #4157ff;
  z-index: 3000;
  color: #ffffff;
}
.share_QRcode {
  height: 36px;
  color: #ffffff;
  font-size: 10px !important;
}
.share_QRcode :hover {
  color: #eef;
}
.emqrcode :hover {
  // background-color: rgb(167, 206, 255);
}
.transition-box {
  background-color: #4157ff;
  text-align: center;
  color: #fff;
  padding: 0 2px;
  box-sizing: border-box;
  border: none;
}
.linl_style {
  color: #4157ff;
  font-size: 12px;
}
</style>
<style lang="less">
#qrcode {
  img {
    margin: 0 auto;
  }
}
</style>

效果如图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值