npm install html2canvas --save
需要的页面引入
import html2canvas from 'html2canvas';
<el-button @click="downBtn">下载</el-button>
<div id="test">
<img v-for="i in 5" :key="i" width="100%" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F022620104202%2F200226104202-10-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642314790&t=78be48ae519c412ceb1b9545ced21a8b" alt="">
</div>
downBtn(){
let dom = document.getElementById('test')
html2canvas(dom, {
useCORS: true,
allowTaint: false,
logging: false,
letterRendering: true,
}).then(canvas => {
this.posterImg = canvas.toDataURL('image/png')
let a = document.createElement("a"); // 生成一个a元素
let event = new MouseEvent("click"); // 创建一个单击事件
a.download = "photo"; // 设置图片名称
a.href = that.posterImg; // 将生成的URL设置为a.href属性
a.dispatchEvent(event); // 触发a的单击事件
})
}