<template><div><buttontype="button"@click="save()">保存</button><aid="link"></a><divclass="course-container"id="myImage"><divclass="course"><imgsrc="@/assets/course.jpeg"/></div><divclass="code"><imgsrc="@/assets/code.jpg"/></div></div></div></template><script>import html2canvas from'html2canvas'exportdefault{data(){return{}},created(){},destroyed(){},methods:{save(){html2canvas(document.querySelector("#myImage")).then(canvas=>{var image = canvas.toDataURL("image/png").replace("image/png","image/octet-stream");// here is the most important part because if you dont replace you will get a DOM 18 exception.//save as download without name and extension//window.location.href = image; var link = document.getElementById('link');link.setAttribute('download','my.png');link.setAttribute('href', canvas.toDataURL("image/png").replace("image/png","image/octet-stream"));link.click();});}}}</script><stylescoped>.course-container{height: 1024px;width: 724px;}.course{z-index: 1;position: absolute;}.code{z-index: 2;position: absolute;margin-top: 700px;margin-left: 400px;width: 150px;height: 150px;}</style>