uniapp微信H5 dom转换成图片并下载(html2canvas )

由于微信安全防控,我们无法直接生成并下载,但是可以打开图片后长按保存

1、npm依赖

 npm install html2canvas --save

2、引入

	import html2canvas from "html2canvas"

3、方法

exportCardAsImage() {
				const cardElement = this.$refs.card.$el;
				console.log(this.$refs.card);
				html2canvas(cardElement).then((canvas) => {
					const imageData = canvas.toDataURL('image/png');
					console.log("图片", imageData);
					this.pic(imageData)
				});
			},
			pic(src) {
				uni.previewImage({
					urls: Array.isArray(src) ? src : [src],
					current: '',
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				});
			},

4、完整代码






你可能感兴趣的:(uni-app)