首先安装
npm install --save html2canvas然后引入
import html2canvas from "html2canvas"这个是方法
第一个参数为dom第二个是导出图片名称
export function downloadDom(dom, fileName) {
html2canvas(dom).then(canvas => {
const a = document.createElement('a')
a.href = canvas.toDataURL("image/jpeg");
// a.download 后面的内容为自定义图片的名称
a.download = fileName || 'download'
a.click()
})
}调用即可

文章介绍了如何通过npm安装html2canvas库,然后导入并使用该库将DOM转换为图片。提供的函数downloadDom接受两个参数,分别是dom元素和图片文件名,生成JPEG图片并自动下载。
1722

被折叠的 条评论
为什么被折叠?



