Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

242.[vue]vue中将html页面转为图片并且下载该图片 #242

Open
webVueBlog opened this issue Jul 16, 2020 · 0 comments
Open

242.[vue]vue中将html页面转为图片并且下载该图片 #242

webVueBlog opened this issue Jul 16, 2020 · 0 comments
Labels
vue vue

Comments

@webVueBlog
Copy link
Member

1.下载 html2canvas

npm install html2canvas

2.对应页面引入该插件

import html2canvas from 'html2canvas';
toImage() {
    html2canvas(this.$refs.imageDom, {
        backgroundColor: '#ffffff'
    }).then(canvas => {
        var imgData = canvas.toDataURL("image/jpeg");
        this.fileDownload(imgData);
    })
},
fileDownload(downloadUrl) {
    let aLink = document.createElement("a");
    aLink.style.display = "none";
    aLink.href = downloadUrl;
    aLink.download = "监控详情.png";
    // 触发点击-然后移除
    document.body.appendChild(aLink);
    aLink.click();
    document.body.removeChild(aLink);
},
<template>
	<div>
		<div class="container" ref="imageDom">hahahah</div>
		<button @click="toImage">导出</button>
	</div>
</template>
import html2canvas from 'html2canvas';

npm install html2canvas
@webVueBlog webVueBlog added the vue vue label Jul 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vue vue
Projects
None yet
Development

No branches or pull requests

1 participant