<div class="canDiv" ref="canDiv" v-show="haiBa">
<view class="canDiv1">
<img ref="imhBx" :src="canvasImg.img" mode=""></img>
</view>
<view class="canDiv2">{{canvasImg.name}}</view>
<view class="canDiv3">
<image :src="canvasImg.erWeima" mode=""></image>
</view>
<view class="canDiv4">长按图片保存到手机</view>
</div>
<view class="pl1" @click="shareBtn">
<span class="iconfont foSiz20"></span>分享
</view>
弹出层位置
<uni-popup ref="popupCan" type="center">
<view class="popImg">
<image class="imgPop" :src="imgsBox" mode="aspectFit" style="z-index: 1000;"></image>
</view>
</uni-popup>
http:
https:
Install NPM
npm install --save html2canvas
Install Yarn
yarn add html2canvas
import html2canvas from 'html2canvas';
import qrcode from '../../utils/weapp-qrcode.js'
data() {
return {
haiBa:false,
canvasImg: {
img: '',
name: '',
erWeima: ''
}
}
}
onLoad(e) {
let erCode = window.location.href
let params = erCode
var imgData = qrcode.drawImg(params, {
typeNumber: 4,
errorCorrectLevel: 'L',
size: 800,
})
this.canvasImg.erWeima = imgData
},
methods: {
generatorImage() {
html2canvas(this.$refs.canDiv).then(canvas => {
this.imgsBox = canvas.toDataURL()
});
},
shareBtn() {
this.haiBa = true
uni.showLoading({
title: '生成中...'
})
setTimeout(() => {
uni.hideLoading()
this.generatorImage()
this.$refs.popupCan.open()
this.haiBa = false
}, 500)
},
}
补充一下小程序问题,小程序中,uniapp的ref要绑定在子组件中才能被获取,如果绑定在view,是获取不了的,你得把业务写在一个组件来引用才行。h5则没有这种情况。