宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

1. 安装包

npm i qrcodejs2 -S

2. 项目中使用

HTML:

!-- 二维码弹框 -->
<!-- 我的二维码是在弹框里,使用的话只需要给一个装二维码的元素就可以 -->
<el-button type="primary" @click="payOrder">生成二维码</el-button>
<el-dialog
width="30%"
:title="payment"
@close="closeCode"
:visible.sync="innerVisible"
append-to-body>
<div class="paycode">
<!-- 放置二维码的容器,需要给一个ref -->
    <div id="qrcode" ref="qrcode"></div>
</div>
</el-dialog>

js:

// 引入
import QRCode from 'qrcodejs2'

methods: {
  // 展示二维码
  payOrder ) {
    this.innerVisible = true
    // 二维码内容,一般是由后台返回的跳转链接,这里是写死的一个链接
    this.qrcode = 'https://yuchengkai.cn/docs/frontend/#typeof'
    // 使用$nextTick确保数据渲染
    this.$nextTick) => {
      this.crateQrcode)
    })
  },
  // 生成二维码
  crateQrcode ) {
    this.qr = new QRCode'qrcode', {
       150,
      height: 150, // 高度
      text: this.qrcode // 二维码内容
      // render: 'canvas' // 设置渲染方式(有两种方式 table和canvas,默认是canvas)
      // background: '#f0f'
      // foreground: '#ff0'
    })
    // console.logthis.qrcode)
  },
  // 关闭弹框,清除已经生成的二维码
  closeCode ) {
    this.$refs.qrcode.innerHTML = ''
  }
}

也是刚好需要做到这个功能,于是就网上找了一下;原文链接:https://www.cnblogs.com/steamed-twisted-roll/p/11271829.html