WeChat applet click to generate QR code function

First look at the effect:

Step 1: Place the required js file  weapp.qrcode.js into utils

Step 2: wxml:


<view>
  <button bindtap='createQrcode' type="primary">生成二维码</button>
  <canvas id='qrcode' type="2d" style='width:300rpx;height:300rpx;margin-top: 30rpx;margin-left: 100rpx;' ></canvas>
</view>

 Step 3: JS

import QRCode from '../../utils/weapp.qrcode'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrCodeLink:'微信小程序生成二维码,你会了吗?',
    qrcodePath:'哪里不会点哪里'
  },
// 生成二维码
createQrcode() {
  var that = this;
  const query = wx.createSelectorQuery()
  query.select('#qrcode')
    .fields({
      node: true,
      size: true
    })
    .exec((res) => {
      var canvas = res[0].node
 
      // 调用方法drawQrcode生成二维码
      QRCode({
        canvas: canvas,
        canvasId: 'qrcode',
        text: that.data.qrCodeLink,
      })
 
      // 获取临时路径(得到之后,想干嘛就干嘛了)
      wx.canvasToTempFilePath({
        canvasId: 'qrcode',
        canvas: canvas,
        x: 0,
        y: 0,
        success(res) {
          // console.log('二维码临时路径:', res.tempFilePath)
          that.setData({
            qrcodePath: res.tempFilePath
          })
          console.log('二维码临时路径:', that.data.qrcodePath)
        },
        fail(res) {
          console.error(res)
        }
      })
    })
},
 
})

At this point, the function of generating the QR code has been realized

However, by scanning this QR code, you found that it is not a jump link, but a piece of text?

Don't panic, that's because the content generated by the qrCodeLink QR code in  your data is text

As long as you change this parameter to a link, you can jump normally         (#^.^#)

Guess you like

Origin blog.csdn.net/qq_17211063/article/details/130641264