html2canvas - Screenshots with JavaScripthttps://html2canvas.hertzen.com/了解一下,安装先
npm install --save html2canvas
用到的知识包括
前端用原生js编辑文件内容→创建生成文件(格式可以自定义)→下载文件_你挚爱的强哥的博客-CSDN博客会自动创建一个html文件。https://blog.csdn.net/qq_37860634/article/details/131571646用例代码
<template>
<div>
<el-button type="primary" ref="btn" @click="downloadImg({ dom: $el, fileName: '自定义图片名称.png' })">下载当前网页</el-button>
</div>
</template>
<script>
import html2canvas from 'html2canvas'; // npm install --save html2canvas (官网:https://html2canvas.hertzen.com)
export default {
methods: {
downloadImg({ dom, fileName = '', fileType = 'image/png', quality = 1.0 } = {}) {
html2canvas(dom, { width: dom.scrollWidth, height: dom.scrollHeight, }).then(canvas => { let a = document.createElement('a'); a.download = fileName, a.href = canvas.toDataURL(fileType, quality), a.click(); });
},
}
};
</script>