导出的页面组件如下:
<template>
<div id="resumeId">
<resumeHtml ref="resume" @on-download="download"/>
</div>
</template>
1、导出html
方法:
1)获取要导出的组件页面的css把它设置成js变量一文本并通过export导出
2)获取要导出组件页面的html的dom标签代码,通过this.$refs.resume.$el.innerHTML获取,也可以通过document.getElementById('resumeId')获得
3)构造html页面,并使用createObjectURL构造一个文件流并下载,如下:
var a = document.createElement('a');
var url = window.URL.createObjectURL(new Blob([content],
{ type: (option.type || "text/plain") + ";charset=" + (option.encoding || 'utf-8') }));
a.href = url;
a.download = fileName || 'file';
a.click();
window.URL.revokeObjectURL(url);
具体代码如下:
import axios from 'axios'
import resumeHtml from './resume-html'
import writer from 'file-writer';
import {resumecss} from '@/assets/style/download/resume.css.js'
...
downloadHtml(name){
let html = this.getHtmlContent();
let s = writer(`${name}的简历.html`, html, 'utf-8');
console.log('s stream',s);
},
getHtmlContent(){
//获取html另外一种方式:this.$el.outerHTML
const template = this.$refs.resume.$el.innerHTML;